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__(s elf, 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 1447
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__(s elf, 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*********@mo notypeimaging.c om> 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__(s elf, 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
/4a53d2c69209ba7 6/9b21a8326d0ef00 2 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
def foo(self, data):
self.attr1=data
self.attr2.append(data)
The initialization of attr1 is obviously OK, all instances of Father
redefine it in the method foo. But the initialization of attr2 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 pickle it?
Thanks ,
Fedor
|
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 situation.
I have the following class structure:
ASSEMBLY ATools
|
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 form it.
I guess that I'll have to use reflection but I can't find any examples on
MSDN.
Thanks in advance,
Mark
|
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 far but its not working...
a =
| |
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:
Point=superTuple("x","y","z") # this is a class factory
p=Point(4,7,9)
assert p.x==p
|
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 questions are stimulated by
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439
Looking at that, what fails if I leave out the following line? ::
|
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
uniform_tuple <3, doublet; ?
Alex Vinokur
|
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 reasonable since generators require
iteration and stop iteration and what not.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |