您的位置:首页 > 编程语言 > Python开发

Python魔术方法详解

2017-09-22 12:59 246 查看
准备工作

为了确保类是新型类,应该把 _metaclass_=type 入到你的模块的最开始。

1 class NewType(Object):
2   mor_code_here
3 class OldType:
4   mor_code_here


 

在这个两个类中NewType是新类,OldType是属于旧类,如果前面加上 _metaclass_=type ,那么两个类都属于新类。

 

构造方法

构造方法与其的方法不一样,当一个对象被创建会立即调用构造方法。创建一个python的构造方法很简答,只要把init方法,从简单的init方法,转换成魔法版本的_init_方法就可以了。

1 class FooBar:
2     def __init__(self):
3         self.somevar = 42
4
5 >>> f =FooBar()
6 >>> f.somevar
7 42


 

重写一个一般方法

每一个类都可能拥有一个或多个超类(父类),它们从超类那里继承行为方法。

1 class A:
2     def hello(self):
3         print 'hello . I am A.'
4 class B(A):
5   pass
6 >>> a = A()
7 >>> b = B()
8 >>> a.hello()
9 hello . I am A.


 

因为B类没有hello方法,B类继承了A类,所以会调用A 类的hello方法。

在子类中增加功能功能的最基本的方式就是增加方法。但是也可以重写一些超类的方法来自定义继承的行为。如下:

1 class A:
2     def hello(self):
3         print 'hello . I am A.'
4 class B(A):
5     def hello(self):
6         print 'hello . I am  B'
7 >>> b = B()
8 >>> b.hello()
9 hello . I am  B


 

特殊的和构造方法

重写是继承机制中的一个重要内容,对一于构造方法尤其重要。看下面的例子:

1 class A:
2     def hello(self):
3         print 'hello . I am A.'
4 class B(A):
5     def hello(self):
6         print 'hello . I am  B'
7 >>> b = B()
8 >>> b.hello()
9 hello . I am  B


 

这个类中定义了鸟有吃的能力, 当它吃过一次后再次就会不饿了,通过上面的执行结果可以清晰的看到。

那么用SongBird类来继承Bird 类,并且给它添加歌唱的方法:

1 class Bird:
2     def __init__(self):
3         self.hungry = True
4     def eat(self):
5         if self.hungry:
6             print 'Aaaah...'
7             self.hungry = False
8         else:
9             print 'No, thanks!'
10
11 class SongBird(Bird):
12          def __init__(self):
13                  self.sound = 'Squawk!'
14          def sing(self):
15                  print self.sound
16 >>> s = SongBird()
17 >>> s.sing()
18 Squawk!
19 >>> s.eat()
20 Traceback (most recent call last):
21   File "<pyshell#26>", line 1, in <module>
22     s.eat()
23   File "C:/Python27/bird", line 6, in eat
24     if self.hungry:
25 AttributeError: 'SongBird' object has no attribute 'hungry'


 

异常很清楚地说明了错误:SongBird没有hungry特性。原因是这样的:在SongBird中,构造方法被重写,但新的构造方法没有任何关于初始化hungry特性的代码。为了达到预期的效果,SongBird的构造方法必须调用其超类Bird的构造方法来确保进行基本的初始化。

两种方法实现:

一 、调用未绑定的超类构造方法

1 class Bird:
2     def __init__(self):
3         self.hungry = True
4     def eat(self):
5         if self.hungry:
6             print 'Aaaah...'
7             self.hungry = False
8         else:
9             print 'No, thanks!'
10
11 class SongBird(Bird):
12          def __init__(self):
13                  Bird.__init__(self)
14                  self.sound = 'Squawk!'
15          def sing(self):
16                  print self.sound
17 >>> s = SongBird()
18 >>> s.sing()
19 Squawk!
20 >>> s.eat()
21 Aaaah...
22 >>> s.eat()
23 No, thanks!


 

在SongBird类中添加了一行代码Bird.__init__(self) 。 在调用一个实例的方法时,该方法的self参数会被自动绑定到实例上(这称为绑定方法)。但如果直接调用类的方法,那么就没有实例会被绑定。这样就可以自由地提供需要的self参数(这样的方法称为未绑定方法)。

通过将当前的实例作为self参数提供给未绑定方法,SongBird就能够使用其超类构造方法的所有实现,也就是说属性hungry能被设置。

二、使用super函数

1 __metaclass__ = type  #表明为新式类
2 class Bird:
3     def __init__(self):
4         self.hungry = True
5     def eat(self):
6         if self.hungry:
7             print 'Aaaah...'
8             self.hungry = False
9         else:
10             print 'No, thanks!'
11
12 class SongBird(Bird):
13          def __init__(self):
14                  super(SongBird,self).__init__()
15                  self.sound = 'Squawk!'
16          def sing(self):
17                  print self.sound
18 >>> s.sing()
19 Squawk!
20 >>> s.eat()
21 Aaaah...
22 >>> s.eat()
23 No, thanks!


 

super函数只能在新式类中使用。当前类和对象可以作为super函数的参数使用,调用函数返回的对象的任何方法都是调用超类的方法,而不是当前类的方法。那就可以不同在SongBird的构造方法中使用Bird,而直接使用super(SongBird,self)。

 

属性

访问器是一个简单的方法,它能够使用getHeight 、setHeight 之样的名字来得到或者重绑定一些特性。如果在访问给定的特性时必须要采取一些行动,那么像这样的封装状态变量就很重要。如下:

1 class Rectangle:
2     def __init__(self):
3         self.width = 0
4         self.height = 0
5     def setSize(self,size):
6         self.width , self.height = size
7     def getSize(self):
8         return self.width , self.height
9 >>> r = Rectangle()
10 >>> r.width = 10
11 >>> r.height = 5
12 >>> r.getSize()
13 (10, 5)
14 >>> r.setSize((150,100))
15 >>> r.width
16 150


 

在上面的例子中,getSize和setSize方法一个名为size的假想特性的访问器方法,size是由width 和height构成的元组。

 

property 函数

property函数的使用很简单,如果已经编写了一个像上节的Rectangle 那样的类,那么只要增加一行代码:

1 __metaclass__ = type
2 class Rectangle:
3     def __int__(self):
4         self.width = 0
5         self.height = 0
6     def setSize(self,size):
7         self.width, self.height = size
8     def getSize(self):
9         return self.width ,self.height
10     size = property(getSize ,setSize)
11 >>> r = Rectangle()
12 >>> r.width = 10
13 >>> r.height = 5
14 >>> r.size
15 (10, 5)
16 >>> r.size = 150,100
17 >>> r.width
18 150


 

在这个新版的Retangle 中,property 函数创建了一个属性,其中访问器函数被用作参数(先取值,然后是赋值),这个属性命为size 。这样一来就不再需要担心是怎么实现的了,可以用同样的方式处理width、height 和size。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: