python把戏要领详解
筹备事情
为了确保类是新型类,应该把 _metaclass_=type 入到你的模块的最开始。
class NewType(Object): mor_code_here class OldType: mor_code_here
在这个两个类中NewType是新类,OldType是属于旧类,假如前面加上 _metaclass_=type ,那么两个类都属于新类。
结构要领
结构要领与其的要领纷歧样,当一个工具被建设会当即挪用结构要领。建设一个python的结构要领很简答,只要把init要领,从简朴的init要领,转换成邪术版本的_init_要领就可以了。
class FooBar:
def __init__(self):
self.somevar = 42
>>> f =FooBar()
>>> f.somevar
42
重写一个一般要领
每一个类都大概拥有一个或多个超类(父类),它们从超类哪里担任行为要领。
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要领。
在子类中增加成果成果的最根基的方法就是增加要领。可是也可以重写一些超类的要领来自界说担任的行为。如下:
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能被配置。
二、利用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!
#p#分页标题#e#
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。