473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Initializing subclasses of tuple

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?

Thanks for any help!
Dave Opstad
Jul 18 '05 #1
5 1432
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/
Jul 18 '05 #2
In article <da*******************************@reader0903.news .uu.net>,
Dave Opstad <da*********@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/..._thread/thread
/4a53d2c69209ba76/9b21a8326d0ef002

http://mail.python.org/pipermail/tut...ry/027779.html

Good luck,

Just
Jul 18 '05 #3
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__

Jul 18 '05 #4
gr*@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/...omization.html

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #5
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...70&atid=105470

STeVe
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
0
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real...
2
by: MR | last post by:
I'm hoping that someone here will be able to direct me to some litrature on how to get a list of a classes subclasses. I.e. need the inherited class to know about subclasses that are inherited...
14
by: KraftDiner | last post by:
I need a matrix of 256 x 256 unsigned short values... The matrix can be implemented as a list of lists or a tuple... So for example: , , ] or ((1,2,3),(4,5,6), (7,8,9)) This is what I have so...
6
by: alainpoint | last post by:
Hello, I have got a problem that i can't readily solve. I want the following: I want to create a supertuple that behaves both as a tuple and as a class. It should do the following:...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.