魔法方法、屬性python
------------------------shell
準備工做ide
爲了確保類是新型類,應該把 _metaclass_=type 入到你的模塊的最開始。函數
class NewType(Object): mor_code_here class OldType: mor_code_here
在這個兩個類中NewType是新類,OldType是屬於舊類,若是前面加上 _metaclass_=type ,那麼兩個類都屬於新類。code
構造方法對象
構造方法與其的方法不同,當一個對象被建立會當即調用構造方法。建立一個python的構造方法很簡答,只要把init方法,從簡單的init方法,轉換成魔法版本的_init_方法就能夠了。繼承
重寫一個通常方法get
每個類均可能擁有一個或多個超類(父類),它們從超類那裏繼承行爲方法。it
class A: def hello(self): print 'hello . I am A.' class B(A): pass >>> a = A() >>> b = B() >>> a.hello() hello . I am A.
由於B類沒有hello方法,B類繼承了A類,因此會調用A 類的hello方法。ast
在子類中增長功能功能的最基本的方式就是增長方法。可是也能夠重寫一些超類的方法來自定義繼承的行爲。以下:
class A: def hello(self): print 'hello . I am A.' class B(A): def hello(self): print 'hello . I am B' >>> b = B() >>> b.hello() hello . I am B
特殊的和構造方法
重寫是繼承機制中的一個重要內容,對一於構造方法尤爲重要。看下面的例子:
class Bird: def __init__(self): self.hungry = True def eat(self): if self.hungry: print 'Aaaah...' self.hungry = False else: print 'No, thanks!' >>> b = Bird() >>> b.eat() Aaaah... >>> b.eat() No, thanks!
這個類中定義了鳥有吃的能力, 當它吃過一次後再次就會不餓了,經過上面的執行結果能夠清晰的看到。
那麼用SongBird類來繼承Bird 類,而且給它添加歌唱的方法:
class Bird: def __init__(self): self.hungry = True def eat(self): if self.hungry: print 'Aaaah...' self.hungry = False else: print 'No, thanks!' class SongBird(Bird): def __init__(self): self.sound = 'Squawk!' def sing(self): print self.sound >>> s = SongBird() >>> s.sing() Squawk! >>> s.eat() Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> s.eat() File "C:/Python27/bird", line 6, in eat if self.hungry: AttributeError: 'SongBird' object has no attribute 'hungry'
異常很清楚地說明了錯誤:SongBird沒有hungry特性。緣由是這樣的:在SongBird中,構造方法被重寫,但新的構造方法沒有任何關於初始化hungry特性的代碼。爲了達到預期的效果,SongBird的構造方法必須調用其超類Bird的構造方法來確保進行基本的初始化。
兩種方法實現:
一 、調用未綁定的超類構造方法
class Bird: def __init__(self): self.hungry = True def eat(self): if self.hungry: print 'Aaaah...' self.hungry = False else: print 'No, thanks!' class SongBird(Bird): def __init__(self): Bird.__init__(self) self.sound = 'Squawk!' def sing(self): print self.sound >>> s = SongBird() >>> s.sing() Squawk! >>> s.eat() Aaaah... >>> s.eat() No, thanks!
在SongBird類中添加了一行代碼Bird.__init__(self) 。 在調用一個實例的方法時,該方法的self參數會被自動綁定到實例上(這稱爲綁定方法)。但若是直接調用類的方法,那麼就沒有實例會被綁定。這樣就能夠自由地提供須要的self參數(這樣的方法稱爲未綁定方法)。
經過將當前的實例做爲self參數提供給未綁定方法,SongBird就可以使用其超類構造方法的全部實現,也就是說屬性hungry能被設置。
2、使用super函數
__metaclass__ = type #代表爲新式類 class Bird: def __init__(self): self.hungry = True def eat(self): if self.hungry: print 'Aaaah...' self.hungry = False else: print 'No, thanks!' class SongBird(Bird): def __init__(self): super(SongBird,self).__init__() self.sound = 'Squawk!' def sing(self): print self.sound >>> s.sing() Squawk! >>> s.eat() Aaaah... >>> s.eat() No, thanks!
super函數只能在新式類中使用。當前類和對象能夠做爲super函數的參數使用,調用函數返回的對象的任何方法都是調用超類的方法,而不是當前類的方法。那就能夠不一樣在SongBird的構造方法中使用Bird,而直接使用super(SongBird,self)。
屬性
訪問器是一個簡單的方法,它可以使用getHeight 、setHeight 之樣的名字來獲得或者重綁定一些特性。若是在訪問給定的特性時必需要採起一些行動,那麼像這樣的封裝狀態變量就很重要。以下:
class Rectangle: def __init__(self): self.width = 0 self.height = 0 def setSize(self,size): self.width , self.height = size def getSize(self): return self.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是由width 和height構成的元組。
property 函數
property函數的使用很簡單,若是已經編寫了一個像上節的Rectangle 那樣的類,那麼只要增長一行代碼:
__metaclass__ = type class Rectangle: def __int__(self): self.width = 0 self.height = 0 def setSize(self,size): self.width, self.height = size def getSize(self): return self.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 函數建立了一個屬性,其中訪問器函數被用做參數(先取值,而後是賦值),這個屬性命爲size 。這樣一來就再也不須要擔憂是怎麼實現的了,能夠用一樣的方式處理width、height 和size。