[Tutor] Basic class inheritance

Cedric BRINER work at infomaniak.ch
Thu Jul 28 17:26:02 CEST 2005


> > However, is it possible to achieve this without rewrite the whole
> > __init__ method, but just overriding parts of it?
> 
> The usual way to do this is to forward to the __init__() method of the superclass for the common part. In your case you are just specializing the default arguments so all you have to do is pass the args to A.__init__():
> 
> class B(A):
>   def __init__(self, blank=True, editable=True, name='foo'):
>     A.__init__(self, blank, editable, name)
> 

I thought such kind of thing should be writted like:
class A(object):
     def __init__(self, blank=False, editable=True, name='foo'):
             self.blank = blank
             self.editable = editable
             self.name = name

class B(A):
  def __init__(self, blank=True, editable=True, name='foo'):
    super(B, self).__init__(blank, editable, name)


Ced.

-- 

Cedric BRINER
Geneva - Switzerland


More information about the Tutor mailing list