类下的方法

 1 class Brand:
 2 
 3     def __init__(self,name):
 4         self.name = name
 5 
 6     def __getitem__(self, item):
 7         print("获取key",item)
 8         print(self.__dict__[item])
 9 
10     def __setitem__(self, key, value):
11         print("设置一个key",key)
12         self.__dict__[key] = value
13 
14     def __delitem__(self, key):
15         print('del obj[key]时,我执行')
16         self.__dict__.pop(key)
17 
18     def __delattr__(self, item):
19         print('del obj.key时,我执行')
20         self.__dict__.pop(item)
21 
22 
23 
24 b = Brand("小猿圈")
25 b['slogan']="自学编程谁不爱小猿圈..."
26 b['website']="apeland.cn"
27 
28 del b["website"]
29 b['name']='小猿圈Apeland..'
30 
31 print(b.__dict__)

 

posted @ 2019-08-28 11:05  Aiden_Shao  阅读(114)  评论(0编辑  收藏  举报