[Tutor] Little subclass understanding problem

Kent Johnson kent37 at tds.net
Thu Nov 15 20:26:55 CET 2007


Tom wrote:
> I am trying to understand what happens in the following scenario:
> 
> class Sub_class(Base_class):
>     def __init__(self, data):
>         Base_class.__init__(self, data)
> 
> as in:
> 
> # snippet from http://viner.tv/go?set
> class Set(list):
>     def __init__(self, value = []):
>         list.__init__([])

This is a bug, it should be
   list.__init__(self, value)

> I have just re-read the relevant part of Wesley Chun's 'Core' book,
> again, and think I *may* understand.
> 
> Base_class.__init__(self, data)
> 
> is *kind of* like saying:
> 
> self.Base_class.__init__(data)
> 
> i.e. it's doing something *to* self. 

Yes, that is more or less correct.

In general, if c is an instance of class C, a method call
   c.foo()

can also be written as
   C.foo(c)

When you want to call a base class method, you have to use the second 
form. That is what
   Base_class.__init__(self)
is doing.

Kent


More information about the Tutor mailing list