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 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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |