Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > overriding a tuple's __init__

Reply
Thread Tools

overriding a tuple's __init__

 
 
Simon Burton
Guest
Posts: n/a
 
      08-18-2003


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.

 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      08-18-2003
In article <(E-Mail Removed)>,
Simon Burton <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
 
Reply With Quote
 
 
 
 
Simon Burton
Guest
Posts: n/a
 
      08-18-2003
On Mon, 18 Aug 2003 08:24:21 +0000, Duncan Booth wrote:

> Simon Burton <(E-Mail Removed)> wrote in
> news(E-Mail Removed):
>
>>>>> 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.

 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      08-18-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      08-18-2003
In article <bhq648$s4t$(E-Mail Removed)>,
Andrew Dalke <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      08-18-2003
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
(E-Mail Removed)


 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      08-18-2003
"Andrew Dalke" <(E-Mail Removed)> wrote in message news:<bhq648$s4t$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      08-18-2003
On 18 Aug 2003 10:27:47 -0400, (E-Mail Removed) (Aahz) wrote:

>In article <bhq648$s4t$(E-Mail Removed)>,
>Andrew Dalke <(E-Mail Removed)> 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
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      08-18-2003
In article <bhricm$v4d$0@216.39.172.122>, Bengt Richter <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should one always add super().__init__() to the __init__? Ramchandra Apte Python 17 09-30-2012 12:04 PM
Overriding __init__() questions grocery_stocker Python 1 04-11-2009 09:42 PM
Overriding member methods in __init__ c james Python 6 12-03-2007 07:28 PM
super(...).__init__() vs Base.__init__(self) Kent Johnson Python 7 02-12-2006 08:59 PM
__new__ does not call __init__ as described in descrintro.html (WAS:Can __new__ prevent __init__ from being called?) Steven Bethard Python 2 02-16-2005 06:50 AM



Advertisments