[Tutor] list.__init__ within class definition

Alan Gauld alan.gauld at btinternet.com
Thu Apr 21 10:02:27 CEST 2011


"James Mills" <prologic at shortcircuit.net.au> wrote

> What you actually want is this:
> 
>>>> class Tomato(list):
> ...     def __init__(self, data):
> ...             super(Tomato, self).__init__(data)
> ...
>>>> l = Tomato([1, 2, 3])
>>>> l
> [1, 2, 3]

> Your example:
> 
>>>> class Tomato(list):
> ...     def __init__(self, data):
> ...             list.__init__(data)
> ...
>>>> l = Tomato([1, 2, 3])
>>>> l
> []
>>>>
> 
> Do you see why ?

This confuses things (at least for me!) It has nothing to 
do with the use of super() but all to do with the use of self.

The OPs style works fuine with self:

>>> class Tomato(list):
...  def __init__(self,data):
...        list.__init__(self,data)
...   
>>> t = Tomato([1,2,3])
>>> t
[1, 2, 3]
>>> 

Using super may be the prefered way of calling the 
superclass but its not the reason the OP code didn't 
work.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list