Dave Opstad 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?

In the __new__ method! This must return the actual created object, whereas __init__ initializes the already-created object.

This applies to subclassing all the built-in types.

regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to