473,399 Members | 3,401 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,399 software developers and data experts.

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
5 7373
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: T. Kaufmann | last post by:
Hi there, A simple but important question: How can I initialize a super class (like dict) correctly in my subclass constructor? A sample: class MyClass(dict): def __init__(self):
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
32
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in...
18
by: Guinness Mann | last post by:
Greetings, I think I saw an article yesterday in which you told someone not to make an Identity column a primary key. I seem to remember you saying something like "It undermines the entire...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
3
by: Carl J. Van Arsdall | last post by:
From my interpreter prompt: >>> tuple = ("blah") >>> len(tuple) 4 >>> tuple2 = ("blah",) >>> len (tuple2) 1 So why is a tuple containing the string "blah" without the comma of
5
by: KraftDiner | last post by:
I had a structure that looked like this ((0,1), (2, 3), (4, 5), (6,7) I changed my code slightly and now I do this: odd = (1,3,5,7) even = (0,2,4,6) all = zip(even, odd) however the zip...
5
by: Kiran | last post by:
Hi all, I want to make python call some C functions, process them and return them. Ok, I got all the wrapper functions and everything setup right. Here is my problem. What I need to do is to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
Oralloy
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.