知行编程网知行编程网  2022-12-06 22:30 知行编程网 隐藏边栏  1 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python魔术方法详解的相关知识,包括python魔术方法,以及python魔法方法这些编程知识,希望对大家有参考作用。

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>", line1,in <module>
    s.eat()
  File "C:/Python27/bird", line6,in eat
    if self.hungry:
AttributeError:'SongBird' object has no attribute'hungry'

异常清楚地指出了错误:SongBird 没有饥饿特征。原因是这样的:在SongBird中,构造函数被覆盖了,但是新的构造函数没有任何初始化饿特征的代码。为了达到预期的效果,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!

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

属性

访问器是一种简单的方法,可以使用 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 是由宽度和高度组成的元组。

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 函数创建了一个属性,其中 accessor 函数用作参数(首先获取,然后分配),这个属性被命名为 size。这样你就不用再为如何实现而烦恼了,宽度、高度、大小都可以用同样的方式来处理。

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享