About Constructor

Consrtuctor is one of the so called "magic methods" in python.  What separates constructors from ordinary methods, is that the constructors are called automatically right after an object has been created. It contrubutes to proper initializing the instance and makes your codes simpler.

If you override the constructor of a class,which always happend in inheritance,you need to call the constructor of the superclass (the class you inherit from) or risk having an object that isn't properly initialized.

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
>>> sb=SongBird()
>>> sb.eat()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "birds.py", line 6, in eat
if self.hungry:
AttributeError: SongBird instance has no attribute 'hungry'
There are basically two ways of doing this: by calling the unbound version of the superclass's constructor or by using the super function. With current versions of Python,  using the super function is clearly the way to go (and with Python 3.0, it will be even more so). However, much existing code uses the unbound supercalss constructor, so you need to know about it.

1)calling the unbound supercalss constructor
class SongBird(Bird):         
      def __init__(self):
          Bird.__init__(self)
          self.sound = 'Squawk!'
      def sing(self):
          print self.sound

If you retrieve the method directly from the class (such as in Bird.__init__), there is no instance to which to bind. Therefore, you are free to supply any self you want to. Such a method is called unbound.

By supplying the current instance as the self argument to the unbound method, the songbird gets the full treatment from its superclass’s constructor (which means inserting the subclass instance into the base-class self,so that it has its hungry attribute set).

2)using the super function

It is called with the current class and instance as its arguments, and any method you call on the returned object will be fetched from the superclass rather than the current class.

class SongBird(Bird):
      def __init__(self):
          super(SongBird, self).__init__() 
          self.sound = 'Squawk!'
      def sing(self):
          print self.sound
The super function is actually quite smart, so even if you have multiple superclasses, you only need to use super once (provided that all the superclass constructors also use super).  What it actually does is return a super object, which will take care of method resolution for you. When you access an attribute on it, it will look through all your superclasses (and super-superclasses, and so forth until it finds the attribute (or raises an AttributeError). 
If the base-class constructor need more arguments, i n the subclass constructor, the superclass constructor can act  like:
class Bird:
      def __init__(self,arguments):
          pass
class SongBird(Bird):
      def __init__(self):
          super(SongBird, self).__init__(arguments) 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值