strange behaviour when inheriting from tuple

Discussion in 'Python' started by metal, Oct 11, 2009.

  1. metal

    metal Guest

    Environment:

    PythonWin 2.5.4 (r254:67916, Apr 27 2009, 15:41:14) [MSC v.1310 32 bit
    (Intel)] on win32.
    Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
    for further copyright information.

    Evil Code:

    class Foo:
    def __init__(self, *args):
    print args

    Foo(1, 2, 3) # (1, 2, 3), good

    class Bar(tuple):
    def __init__(self, *args):
    print args

    Bar(1, 2, 3) # TypeError: tuple() takes at most 1 argument (3 given)

    what the heck? I even didn't call tuple.__init__ yet
    metal, Oct 11, 2009
    #1
    1. Advertising

  2. metal

    ryles Guest

    On Oct 11, 3:04 am, metal <> wrote:
    > Environment:
    >
    > PythonWin 2.5.4 (r254:67916, Apr 27 2009, 15:41:14) [MSC v.1310 32 bit
    > (Intel)] on win32.
    > Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
    > for further copyright information.
    >
    > Evil Code:
    >
    > class Foo:
    >         def __init__(self, *args):
    >                 print args
    >
    > Foo(1, 2, 3) # (1, 2, 3), good
    >
    > class Bar(tuple):
    >         def __init__(self, *args):
    >                 print args
    >
    > Bar(1, 2, 3) # TypeError: tuple() takes at most 1 argument (3 given)
    >
    > what the heck? I even didn't call tuple.__init__ yet


    When subclassing immutable types you'll want to override __new__, and
    should ensure that the base type's __new__ is called:

    __ class MyTuple(tuple):
    __ def __new__(cls, *args):
    __ return tuple.__new__(cls, args)
    __ print MyTuple(1, 2, 3)

    (1, 2, 3)

    See http://www.python.org/download/releases/2.2.3/descrintro/#__new__
    ryles, Oct 11, 2009
    #2
    1. Advertising

  3. metal

    metal Guest

    On 10ÔÂ11ÈÕ, ÏÂÎç5ʱ30·Ö, ryles <> wrote:
    > On Oct 11, 3:04 am, metal <> wrote:
    >
    >
    >
    > > Environment:

    >
    > > PythonWin 2.5.4 (r254:67916, Apr 27 2009, 15:41:14) [MSC v.1310 32 bit
    > > (Intel)] on win32.
    > > Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
    > > for further copyright information.

    >
    > > Evil Code:

    >
    > > class Foo:
    > > def __init__(self, *args):
    > > print args

    >
    > > Foo(1, 2, 3) # (1, 2, 3), good

    >
    > > class Bar(tuple):
    > > def __init__(self, *args):
    > > print args

    >
    > > Bar(1, 2, 3) # TypeError: tuple() takes at most 1 argument (3 given)

    >
    > > what the heck? I even didn't call tuple.__init__ yet

    >
    > When subclassing immutable types you'll want to override __new__, and
    > should ensure that the base type's __new__ is called:
    >
    > __ class MyTuple(tuple):
    > __ def __new__(cls, *args):
    > __ return tuple.__new__(cls, args)
    > __ print MyTuple(1, 2, 3)
    >
    > (1, 2, 3)
    >
    > Seehttp://www.python.org/download/releases/2.2.3/descrintro/#__new__


    That's it. Thank you very much.
    metal, Oct 11, 2009
    #3
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    804
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    950
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    611
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    819
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    959
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page