467,886 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,886 developers. It's quick & easy.

Question about extending tuple

I wanted to extend tuple but ran into a problem. Here is what I
thought would work

class MyTuple(tuple):
def __init__(self, *args):
tuple.__init__(self, args)

x = MyTuple(1,2,3,4)

That gives me...

TypeError: tuple() takes at most 1 argument (4 given).

However, this call works:

x = MyTuple((1,2,3,4))

I am perplexed only because "args" is a tuple by the definition of
*args. Anyone?

Mar 28 '07 #1
  • viewed: 6727
Share:
5 Replies
abcd schrieb:
I wanted to extend tuple but ran into a problem. Here is what I
thought would work

class MyTuple(tuple):
def __init__(self, *args):
tuple.__init__(self, args)

x = MyTuple(1,2,3,4)

That gives me...

TypeError: tuple() takes at most 1 argument (4 given).

However, this call works:

x = MyTuple((1,2,3,4))

I am perplexed only because "args" is a tuple by the definition of
*args. Anyone?
As an immutable type, tuple makes use of __new__.

class MyTuple(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)

should work.

Georg

Mar 28 '07 #2
abcd <co*******@gmail.comwrote:
I wanted to extend tuple but ran into a problem. Here is what I
thought would work
I think you should take a look at this to do it properly from the Python
devs:
http://svn.python.org/view/python/tr...collections.py

Look for NamedTuple

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Mar 28 '07 #3
As an immutable type, tuple makes use of __new__.
>
class MyTuple(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)

should work.

Georg
strange. not very consistent.

Mar 28 '07 #4
abcd schrieb:
>As an immutable type, tuple makes use of __new__.

class MyTuple(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)

should work.

Georg

strange. not very consistent.
On the contrary -- __new__ *and* __init__ exist for all types.
The only difference is where a specific object is initialized, and
therefore which method you have to override.

__new__ is a static method (it doesn't need to be declared as one,
this is done automatically as it predates the introduction of
staticmethod()) which is called to *construct* an instance.
This can only be done once for a specific object since each call to
__new__ will result in a *new* object. In other words, this is
perfect for immutable objects -- once created, never changed.

__init__, OTOH, is called on the *instance* to initialize it. Of
course, this process can be repeated, and is therefore apt for
mutable objects like lists.

I hope you see now why it is consistent.

Georg

Mar 28 '07 #5
>
I hope you see now why it is consistent.

Georg
yea that clears it up. thanks.

Mar 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by T. Kaufmann | last post: by
50 posts views Thread by Will McGugan | last post: by
32 posts views Thread by David | last post: by
18 posts views Thread by Guinness Mann | last post: by
105 posts views Thread by Christoph Zwerschke | last post: by
3 posts views Thread by Carl J. Van Arsdall | last post: by
5 posts views Thread by KraftDiner | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.