473,382 Members | 1,431 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,382 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 7371
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.