[Tutor] Basic class inheritance

Kent Johnson kent37 at tds.net
Thu Jul 28 17:41:43 CEST 2005


Cedric BRINER wrote:
>>>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)

Yes, that is the more modern way to do it for new-style classes. In the original example, class A does not inherit from object so I used the older style.

Kent



More information about the Tutor mailing list