以下内容,源于个人理解所得,纯属臆测,爱信不信:-D。欢迎大家留言讨论指正。
1、__new__魔法方法:
原型:__new__(cls, *args, **kwargs)
说明:__new__魔法方法用于对象的实例化,作用于__init__初始化对象之前。其中cls为需要实例化的类。new方法必须有返回值,即为实例化后的对象。可以被用于创建单例模式,限制该类实例化的对象个数。
class SingleTon(object): instance = {} def __new__(cls, *args, **kwargs): if cls not in SingleTon.instance: SingleTon.instance[cls] =super(SingleTon,cls).__new__(cls,*args, **kwargs) return SingleTon.instance[cls] def __init__(self, *args, **kwargs): pass if __name__ == '__main__': a = SingleTon() b = SingleTon() if a is b : print "SingleTon" else: print "not SingleTon"
结果输出:SingleTon
2、__doc__属性:
用于查看类或方法的文档
3、__dict__属性:
以字典的形式呈现类或类实例所有的属性
class SingleTon(object): instance = {} def __new__(cls, *args, **kwargs): if cls not in SingleTon.instance: SingleTon.instance[cls] =super(SingleTon,cls).__new__(cls,*args, **kwargs) return SingleTon.instance[cls] def __init__(self, *args, **kwargs): self.var1 = 1 def test(self): print "tee" if __name__ == '__main__': a = SingleTon() b = SingleTon() a.var2 = 2 print a.__dict__ print b.__dict__
结果为:
{'var1': 1, 'var2': 2}
{'var1': 1, 'var2': 2}
4、__getattribute__方法
原型:__getattribute__(self, name)
说明:当name代表的属性或方法被访问时,会先执行这个方法。同时注意这个方法需要类继承自object,即新式类才可以使用。
1 class SingleTon(object): 2 instance = {} 3 def __new__(cls, *args, **kwargs): 4 if cls not in SingleTon.instance: 5 SingleTon.instance[cls] =super(SingleTon,cls).__new__(cls,*args, **kwargs) 6 return SingleTon.instance[cls] 7 def __init__(self, *args, **kwargs): 8 self.var1 = 1 9 def __getattribute__(self,name): 10 print "variable %s is getted"%name 11 return super(SingleTon,self).__getattribute__(name) 12 def test(self): 13 print "test" 14 if __name__ == '__main__': 15 a = SingleTon() 16 a.var1 17 a.test()
结果:
variable var1 is getted
variable test is getted
test
5、__getattr__方法:
原型:__getattr__(self,name)
说明:当类对象调用的属性为找到则会调用这个方法,进行处理。
1 class SingleTon(object): 2 instance = {} 3 def __new__(cls, *args, **kwargs): 4 if cls not in SingleTon.instance: 5 SingleTon.instance[cls] =super(SingleTon,cls).__new__(cls,*args, **kwargs) 6 return SingleTon.instance[cls] 7 def __init__(self, *args, **kwargs): 8 self.var1 = 1 9 def __getattr__(self,name): 10 print "not found %s"%name 11 def __getattribute__(self,name): 12 print "variable %s is getted"%name 13 return super(SingleTon,self).__getattribute__(name) 14 def test(self): 15 print "test" 16 if __name__ == '__main__': 17 a = SingleTon() 18 a.c
结果输出:
variable c is getted
not found c
由上面的例子可以看出__getattribute__魔法方法无论所调用属性方法存在与否都会去调用,而__getattr__方法只会在查找属性不存在的时候才会调用。同时需要注意__getattr__方法晚于__getattribute__方法执行。
6、__setattr__方法:
原型:__setattr__(self, name, value)
说明:对类属性进行赋值的时候,会先调用这个魔法方法,和__getattr__一样,俗称"拦截",可用于添加日志等。
1 class SingleTon(object): 2 instance = {} 3 def __new__(cls, *args, **kwargs): 4 if cls not in SingleTon.instance: 5 SingleTon.instance[cls] =super(SingleTon,cls).__new__(cls,*args, **kwargs) 6 return SingleTon.instance[cls] 7 def __init__(self, *args, **kwargs): 8 self.var1 = 1 9 def __getattr__(self,name): 10 print "not found %s"%name 11 def __getattribute__(self,name): 12 print "variable %s is getted"%name 13 return super(SingleTon,self).__getattribute__(name) 14 def __setattr__(self,name, value): 15 print "variable %s is setted"%name 16 return super(SingleTon,self).__setattr__(name,value) 17 if __name__ == '__main__': 18 a = SingleTon() 19 a.c = 2
结果:
variable var1 is setted
variable c is setted
注:经测试,python没有__setattribute__魔法方法,想想也没有必要,__setattr__足够了。