python魔术方法详解

准备工作
为了保证类是新类,应该把它们放在一边 _metaclass_=type 进入你的模块的开始。
classNewType(Object): mor_code_here classOldType: mor_code_here
Newtype是这两个类别中的一个新类别,Oldtype属于旧类别 _metaclass_=type ,所以两类都属于新类。
构造方法
结构方法不同于其方法。当一个对象被创建时,将立即调用结构方法。创建python的结构方法是一个简单的答案。只要将init方法从简单的init方法转换为魔法版本_init_方法可以。
classFooBar: def__init__(self): self.somevar=42 >>>f=FooBar() >>>f.somevar 42
重写一般方法
每个类别都可能有一个或多个超级类别(父类),它们从超级类别中继承行为。
classA: defhello(self): print'hello.IamA.' classB(A): pass >>>a=A() >>>b=B() >>>a.hello() hello.IamA.
因为B类没有hello方法,B类继承了A类,所以会调用A类 类hello方法。
增加子类功能最基本的方法是增加子类功能。但也可以从定义继承的行为中重写一些超级方法。如下:
classA: defhello(self): print'hello.IamA.' classB(A): defhello(self): print'hello.IamB' >>>b=B() >>>b.hello() hello.IamB
特殊的结构和结构方法
重写是继承机制的重要组成部分,对于结构方法尤为重要。见下面的例子:
classBird: def__init__(self): self.hungry=True defeat(self): ifself.hungry: print'Aaaah...' self.hungry=False else: print'No,thanks!' >>>b=Bird() >>>b.eat() Aaaah... >>>b.eat() No,thanks!
这一类定义了鸟有吃的能力, 当它再次食用时,它就不会饿了,通过上述执行结果可以清楚地看到。
然后用SongBird类继承Bird 类,并添加歌唱方法:
classBird: def__init__(self): self.hungry=True defeat(self): ifself.hungry: print'Aaaah...' self.hungry=False else: print'No,thanks!' classSongBird(Bird): def__init__(self): self.sound='Squawk!' defsing(self): printself.sound >>>s=SongBird() >>>s.sing() Squawk! >>>s.eat() Traceback(mostrecentcalllast): File"<pyshell#26>",line1,in<module> s.eat() File"C:/Python27/bird",line6,ineat ifself.hungry: AttributeError:'SongBird'objecthasnoattribute'hungry'
非常明显的错误:SongBird没有hungry特性。原因是:在SongBird中,重写了结构方法,但新的结构方法没有关于初始化hungry特性的代码。为了达到预期的效果,SongBird的结构方法必须调用其超级Bird的结构方法,以确保基本的初始化。
实现两种方法:
一 、调用未绑定的超类结构方法
classBird: def__init__(self): self.hungry=True defeat(self): ifself.hungry: print'Aaaah...' self.hungry=False else: print'No,thanks!' classSongBird(Bird): def__init__(self): Bird.__init__(self) self.sound='Squawk!' defsing(self): printself.sound >>>s=SongBird() >>>s.sing() Squawk! >>>s.eat() Aaaah... >>>s.eat() No,thanks!
在SongBird类中添加一行代码Bird.__init__(self) 。 调用实例时,该方法的self参数会自动绑定到实例上(称为绑定方法)。但是如果直接调用类的方法,就没有实例会被绑定。这样就可以自由提供所需的self参数(这种方法叫未绑定方法)。
通过向未绑定方法提供当前示例作为self参数,SongBird可以使用其超级结构方法的所有实现,也就是说,可以设置属性hungry。
二、使用super函数
__metaclass__=type#表示为新类 classBird: def__init__(self): self.hungry=True defeat(self): ifself.hungry: print'Aaaah...' self.hungry=False else: print'No,thanks!' classSongBird(Bird): def__init__(self): super(SongBird,self).__init__() self.sound='Squawk!' defsing(self): printself.sound >>>s.sing() Squawk! >>>s.eat() Aaaah... >>>s.eat() No,thanks!
super函数只能在新类中使用。当前类和对象可以作为super函数的参数使用,任何调用函数返回对象的方法都是调用超级类的方法,而不是当前类的方法。然后在songbird的结构方法中使用不同的bird,直接使用superd(SongBird,self)。
属性
访问器是一种使用getheight的简单方法 、setHeight 这样的名字可以获得或重新绑定一些特征。如果在访问给定的特征时必须采取一些行动,则此类包装状态的变量非常重要。如下:
classRectangle: def__init__(self): self.width=0 self.height=0 defsetSize(self,size): self.width,self.height=size defgetSize(self): returnself.width,self.height >>>r=Rectangle() >>>r.width=10 >>>r.height=5 >>>r.getSize() (10,5) >>>r.setSize((150,100)) >>>r.width 150
在上面的例子中,getsize和setsize方法是一种名为size假想特征的访问器方法,size是由widthize组成的 由height组成的元组。
property 函数
使用property函数非常简单,如果已经编写了一个像上节一样的Rectangle 这样的类,只需添加一行代码:
__metaclass__=type classRectangle: def__int__(self): self.width=0 self.height=0 defsetSize(self,size): self.width,self.height=size defgetSize(self): returnself.width,self.height size=property(getSize,setSize) >>>r=Rectangle() >>>r.width=10 >>>r.height=5 >>>r.size (10,5) >>>r.size=150,100 >>>r.width 150
在这个新版本的Retangle中 中,property 函数创建了一个属性,其中访问器函数被用作参数(先取值,再赋值),该属性命为sizee 。这样,就不再需要担心如何实现它了。width可以以同样的方式处理、height 和size。p>
