overriding a tuple's __init__

Discussion in 'Python' started by Simon Burton, Aug 18, 2003.

  1. Simon Burton

    Simon Burton Guest

    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.
     
    Simon Burton, Aug 18, 2003
    #1
    1. Advertising

  2. Simon Burton

    Aahz Guest

    In article <>,
    Simon Burton <> 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 () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Aug 18, 2003
    #2
    1. Advertising

  3. Simon Burton

    Simon Burton Guest

    On Mon, 18 Aug 2003 08:24:21 +0000, Duncan Booth wrote:

    > Simon Burton <> wrote in
    > news:p:
    >
    >>>>> 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.
     
    Simon Burton, Aug 18, 2003
    #3
  4. Simon Burton

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Aug 18, 2003
    #4
  5. Simon Burton

    Aahz Guest

    In article <bhq648$s4t$>,
    Andrew Dalke <> 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 () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Aug 18, 2003
    #5
  6. Simon Burton

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Aug 18, 2003
    #6
  7. "Andrew Dalke" <> wrote in message news:<bhq648$s4t$>...
    > 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
     
    Michele Simionato, Aug 18, 2003
    #7
  8. On 18 Aug 2003 10:27:47 -0400, (Aahz) wrote:

    >In article <bhq648$s4t$>,
    >Andrew Dalke <> 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
     
    Bengt Richter, Aug 18, 2003
    #8
  9. Simon Burton

    Aahz Guest

    In article <bhricm$v4d$0@216.39.172.122>, Bengt Richter <> 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 () <*> http://www.pythoncraft.com/

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

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Bethard
    Replies:
    2
    Views:
    483
    Steven Bethard
    Feb 16, 2005
  2. Kent Johnson
    Replies:
    7
    Views:
    943
    Jan Niklas Fingerle
    Feb 12, 2006
  3. Alan Isaac

    __init__ in subclass of tuple

    Alan Isaac, Mar 10, 2007, in forum: Python
    Replies:
    2
    Views:
    291
    Alan Isaac
    Mar 10, 2007
  4. c james
    Replies:
    6
    Views:
    329
    c james
    Dec 3, 2007
  5. Ramchandra Apte
    Replies:
    17
    Views:
    367
    Manuel Pégourié-Gonnard
    Sep 30, 2012
Loading...

Share This Page