Initializing subclasses of tuple

2005-03-01 Thread Dave Opstad
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 "", 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?

Thanks for any help!
Dave Opstad
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Initializing subclasses of tuple

2005-03-01 Thread Steve Holden
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 "", 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


Re: Initializing subclasses of tuple

2005-03-01 Thread Just
In article <[EMAIL PROTECTED]>,
 Dave Opstad <[EMAIL PROTECTED]> 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 "", 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Initializing subclasses of tuple

2005-03-01 Thread gry
To inherit from an immutable class, like string or tuple, you need to
use the __new__ member, not __init__.  See, e.g.:

http://www.python.org/2.2.3/descrintro.html#__new__

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Initializing subclasses of tuple

2005-03-02 Thread Nick Coghlan
gry@ll.mit.edu wrote:
To inherit from an immutable class, like string or tuple, you need to
use the __new__ member, not __init__.  See, e.g.:
http://www.python.org/2.2.3/descrintro.html#__new__
Any volunteers out there willing to put some words about __new__ together for 
the main Python docs, please consider posting them on SF :)

It's currently conspicuous by its absence:
http://www.python.org/dev/doc/devel/ref/customization.html
Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: Initializing subclasses of tuple

2005-03-03 Thread Steven Bethard
Nick Coghlan wrote:
Any volunteers out there willing to put some words about __new__ 
together for the main Python docs, please consider posting them on SF :)
Done.
http://sourceforge.net/tracker/?func=detail&aid=1156412&group_id=5470&atid=105470
STeVe
--
http://mail.python.org/mailman/listinfo/python-list