Initializing subclasses of tuple
Just
just at xs4all.nl
Tue Mar 1 11:12:05 EST 2005
In article <dave.opstad-D863D6.08013201032005 at reader0903.news.uu.net>,
Dave Opstad <dave.opstad at monotypeimaging.com> wrote:
> I'm hoping someone can point out where I'm going wrong here. Here's a
> snippet of a Python interactive session (2.3, if it makes a difference):
>
> --------------------------------------
> >>> class X(list):
> ... def __init__(self, n):
> ... v = range(n)
> ... list.__init__(self, v)
> ...
> >>> x = X(10)
> >>> x
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >>> class Y(tuple):
> ... def __init__(self, n):
> ... v = tuple(range(n))
> ... tuple.__init__(self, v)
> ...
> >>> y = Y(10)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: iteration over non-sequence
> --------------------------------------
>
> How do I initialize instances of a class derived from tuple, if it's not
> in the __init__ method?
Hi Dave,
You're going to have to override __new__. See eg.
http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread
/4a53d2c69209ba76/9b21a8326d0ef002
http://mail.python.org/pipermail/tutor/2004-January/027779.html
Good luck,
Just
More information about the Python-list
mailing list