码迷,mamicode.com
首页 > 编程语言 > 详细

python-面向对象之item系列(__getitem__,__setitem__,__delitem__)

时间:2017-11-25 17:28:01      阅读:890      评论:0      收藏:0      [点我收藏+]

标签:div   get   item   面向对象   对象   foo   python   pre   dict   

class Foo:
    def __getitem__(self, item):
        print(‘=====>get‘)
        return self.__dict__[item]

    def __setitem__(self, key, value):
        self.__dict__[key] = value
        # setattr(self,key,value)

    def __delitem__(self, key):
        self.__dict__.pop(key)


f = Foo()
f.x = 1
print(f.x)
print(f.__dict__)

1、增加

f[‘x‘] = 123123123123
print(f.__dict__)
f[‘x‘]

2、删除 

del f[‘x‘]
print(f[‘x‘])

  

python-面向对象之item系列(__getitem__,__setitem__,__delitem__)

标签:div   get   item   面向对象   对象   foo   python   pre   dict   

原文地址:http://www.cnblogs.com/xiechao621/p/7895612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!