类通过索引取值:
class Foo(object): def __getitem__(self, item): return 666obj = Foo()v = obj['xxx'] # __getitem__# obj() # __call__# obj['xxx'] = 999 # __setitem__# del obj['xxx'] # __delitem__"""对象中的内置方法:__init____call____new____getitem____setitem____delitem____add__"""# obj[]自动执行类的__getitem__方法class Bar(object): def __init__(self, num): self.num = num def __add__(self, other): return self.num + other.numa = Bar(1)b = Bar(3)print(a + b)
class Foo(object):
def __init__(self):
#构造方法
def __del__(self):
# 析构方法,python解释器在销毁对象的时候,执行的方法,我们不用管
常用内置方法
http://www.cnblogs.com/Vee-Wang/p/7133183.html
End