类的特殊方法

 1 class foo:
 2     def __init__(self):
 3         print('init')
 4     def __call__(self, *args, **kwargs):
 5         print('call')
 6         return 1
 7     def __getitem__(self, item):
 8         print(item)
 9     def __setitem__(self, key, value):
10         print(key,value)
11     def __delitem__(self, key):
12         print(key)
13 r = foo()
14 r() #__call__
15 r['ffff'] # __getitem__
16 r['fffggdg'] = 123 # __setitem__
17 del r['hfdfbdfb'] # __delitem__

 

class foo:
    def __init__(self,args):
        self.xo = args
    def __str__(self):
        return self.xo
r = foo('出错啦')
print(r)

 

posted @ 2017-02-25 18:22  Erick-LONG  阅读(170)  评论(0编辑  收藏  举报