[Tutor] list.__init__ within class definition

James Mills prologic at shortcircuit.net.au
Thu Apr 21 05:34:07 CEST 2011


On Thu, Apr 21, 2011 at 1:21 PM, Alex Companioni <achompas at gmail.com> wrote:
> In the following class definition:
>
> class Tomato(list):
>    def __init__(self, data):
>        list.__init__(self, data)
>
> The list.__init__ method (if it is a method, I'm not clear on what
> __init__ actually *is*) creates a list, right? In other words,
>
> l = Tomato([1,2,3])
>
> will create a list l with the values [1,2,3], correct?

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 ?

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"


More information about the Tutor mailing list