473,383 Members | 1,864 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,383 software developers and data experts.

overriding a tuple's __init__



Python 2.2.2 (#2, Nov 24 2002, 11:41:06)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class pair(tuple): .... def __init__(self,a,b):
.... tuple.__init__(self, (a,b) )
.... a=pair(1,2) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: tuple() takes at most 1 argument (2 given)


What gives? (yes it works with a list, but i need immutable/hashable)

Simon Burton.

Jul 18 '05 #1
8 2190
On Mon, 18 Aug 2003 08:24:21 +0000, Duncan Booth wrote:
Simon Burton <si****@webone.com.au> wrote in
news:pa****************************@webone.com.au:
> class pair(tuple):

... def __init__(self,a,b):
... tuple.__init__(self, (a,b) )
...
> a=pair(1,2)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: tuple() takes at most 1 argument (2 given)
>


What gives? (yes it works with a list, but i need immutable/hashable)


You need to override __new__ instead of __init__:


:) I need to grow a brain. thanks Duncan.

Simon.

Jul 18 '05 #2
Simon Burton:
class pair(tuple): ... def __init__(self,a,b):
... tuple.__init__(self, (a,b) ) What gives? (yes it works with a list, but i need immutable/hashable)


The problem is the immutability. This one one of the
new changes in 2.3 I still don't fully understand, but I do
know the solution is __new__
class pair(tuple): .... def __new__(self, a, b):
.... return tuple((a, b))
....
pair(2,3) (2, 3) x=_
type(x) <type 'tuple'>


That should give you some pointers for additional searches.

Andrew
da***@dalkescientific.com
Jul 18 '05 #3
In article <pa****************************@webone.com.au>,
Simon Burton <si****@webone.com.au> wrote:

Python 2.2.2 (#2, Nov 24 2002, 11:41:06)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class pair(tuple):... def __init__(self,a,b):
... tuple.__init__(self, (a,b) )
... a=pair(1,2)Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: tuple() takes at most 1 argument (2 given)


What gives? (yes it works with a list, but i need immutable/hashable)


You need to define __new__(); __init__() gets called *after* object
creation, when it's already immutable.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #4
In article <bh**********@slb3.atl.mindspring.net>,
Andrew Dalke <ad****@mindspring.com> wrote:

The problem is the immutability. This one one of the
new changes in 2.3 I still don't fully understand, but I do
know the solution is __new__
class pair(tuple):... def __new__(self, a, b):
... return tuple((a, b))
...
pair(2,3)(2, 3) x=_
type(x)<type 'tuple'>


That should give you some pointers for additional searches.


This works better:

class pair(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #5
Aahz:
class pair(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)


Right. cls instead of self because it isn't passed the instance.

It would help me learn this new part of Python if I had
any use for it. :)

Though *args likely isn't what the OP wanted - I assume
that 'pair' takes only two elements.

Andrew
da***@dalkescientific.com
Jul 18 '05 #6
"Andrew Dalke" <ad****@mindspring.com> wrote in message news:<bh**********@slb3.atl.mindspring.net>...
Simon Burton:
>> class pair(tuple):

... def __init__(self,a,b):
... tuple.__init__(self, (a,b) )

What gives? (yes it works with a list, but i need immutable/hashable)


The problem is the immutability. This one one of the
new changes in 2.3


<nitpick mode> Actually this was a change in 2.2 </nitpick mode>

__new__ is needed to acts on the creation of immutable objects and this
is the right way to use it; unfortunaly it gives room to plenty of abuses:

class YouThinkIamAString(str):
def __new__(cls,arg):
return 42

print YouThinkIamAString("What's the answer?")

Yes, in Python you cannot modify the builtins, but still you have plenty
of rope to shoot in your foot ;)

Michele
Jul 18 '05 #7
On 18 Aug 2003 10:27:47 -0400, aa**@pythoncraft.com (Aahz) wrote:
In article <bh**********@slb3.atl.mindspring.net>,
Andrew Dalke <ad****@mindspring.com> wrote:

The problem is the immutability. This one one of the
new changes in 2.3 I still don't fully understand, but I do
know the solution is __new__
> class pair(tuple):

... def __new__(self, a, b):
... return tuple((a, b))
...
>
> pair(2,3)

(2, 3)
> x=_
> type(x)

<type 'tuple'>
>


That should give you some pointers for additional searches.


This works better:

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


so far, just

class pair(tuple): pass

should do it, no? Unless you want to take the name as suggesting that
length 2 should be enforced. Don't know what other methods are planned,
but ISTM you get the vanilla __new__ for free. Or am I missing something?

Regards,
Bengt Richter
Jul 18 '05 #8
In article <bh**********@216.39.172.122>, Bengt Richter <bo**@oz.net> wrote:

so far, just

class pair(tuple): pass

should do it, no? Unless you want to take the name as suggesting that
length 2 should be enforced. Don't know what other methods are planned,
but ISTM you get the vanilla __new__ for free. Or am I missing something?


Certainly; I'm just illustrating the principle if you wanted to do
something useful. ;-)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michele Simionato | last post by:
Let me show first how does it work for tuples: >>> class MyTuple(tuple): .... def __new__(cls,strng): # implicit conversion string of ints => tuple .... return...
3
by: Christoph Groth | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQBAJmKufx2/njzvX5URAipnAKCUco5ZCl85xJ8YboHHJM3RBIqiGwCfX3/P...
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,...
5
by: Dave Opstad | last post by:
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...
3
by: James Stroud | last post by:
Hello All, Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some subtle logic? Why does print not work the same way as raise? Both are statements. Why does raise need to...
6
by: groups.20.thebriguy | last post by:
I've noticed that there's a few functions that return what appears to be a tuple, but that also has attributes for each item in the tuple. For example, time.localtime() returns a time.time_struct,...
2
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...
5
by: abcd | last post by:
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...
2
by: Pradnyesh Sawant | last post by:
Hello, I have a pyqt4 code in which i'm trying the signal/slot mechanism. The (stripped) code is as follows: class D(QtCore.QThread): def __init__(self): QtCore.QThread.__init__(self) tpl =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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.