possible bug in inheritence from tuple
tim.peters at gmail.com
Thu Sep 9 17:14:35 CEST 2004
> The following code should in my opinion have ended with (1,2,3,4).
> It works fine when inheriting from list.
> Is this a bug in python?
> >>> class Test(tuple):
> ... def __init__(self):
> ... tuple.__init__(self, (1,2,3,4))
> >>> t = Test()
> >>> t
Lists are mutable, tuples are immutable. By the time __init__ is
called, it's too late to set the value of a tuple -- self already
exists (and is immutable -- you no longer change its value). To
initialize an immutable value, you have to override __new__, which, in
effect, *creates* self. This was covered in more detail here within
the last few weeks, but (sorry!) I don't have time to search for it
More information about the Python-list