python基础学习笔记(十)

魔法方法、属性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_方法就能够了。继承

复制代码
class FooBar:
    def __init__(self):
        self.somevar = 42
        
>>> f =FooBar()
>>> f.somevar
42
复制代码

 

 

重写一个通常方法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。