Calling parent constructor with different argument list

Discussion in 'Python' started by pinkisntwell, Aug 14, 2009.

  1. pinkisntwell

    pinkisntwell Guest

    class Vertex(tuple):
    pass

    class Positioned_Vertex(Vertex):

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

    a=Positioned_Vertex((0,0,0), 1)

    This gives:

    TypeError: tuple() takes at most 1 argument (2 given)

    It looks like the explicit call to Vertex.__init__ is never made and
    Vertex.__init__ is implicitly called when a Positioned_Vertex is
    created. Is there a way to work around this and call the constructor
    with the intended argument list?
     
    pinkisntwell, Aug 14, 2009
    #1
    1. Advertising

  2. pinkisntwell wrote:
    > class Vertex(tuple):
    > pass
    >
    > class Positioned_Vertex(Vertex):
    >
    > def __init__(self, a, b):

    def __init__(self, a): # just take out b
    > Vertex.__init__(a)
    >
    > a=Positioned_Vertex((0,0,0), 1)

    a=Positioned_Vertex( ( (0,0,0), 1) ) # and add a pair of brackets
    print a
    >
    > This gives:
    >
    > TypeError: tuple() takes at most 1 argument (2 given)
    >
    > It looks like the explicit call to Vertex.__init__ is never made and
    > Vertex.__init__ is implicitly called when a Positioned_Vertex is
    > created. Is there a way to work around this and call the constructor
    > with the intended argument list?

    Simplest way to get it to work is above using Python 2.6.2 on Windows.
    I'm sure there are variations depending on your use case, but I'll leave
    that to the experts.

    --
    Kindest regards.

    Mark Lawrence.
     
    Mark Lawrence, Aug 15, 2009
    #2
    1. Advertising

  3. En Fri, 14 Aug 2009 19:24:26 -0300, pinkisntwell <>
    escribió:

    > class Vertex(tuple):
    > pass
    >
    > class Positioned_Vertex(Vertex):
    >
    > def __init__(self, a, b):
    > Vertex.__init__(a)
    >
    > a=Positioned_Vertex((0,0,0), 1)
    >
    > This gives:
    >
    > TypeError: tuple() takes at most 1 argument (2 given)
    >
    > It looks like the explicit call to Vertex.__init__ is never made and
    > Vertex.__init__ is implicitly called when a Positioned_Vertex is
    > created. Is there a way to work around this and call the constructor
    > with the intended argument list?


    The tuple constructor (like numbers, strings, and other immutable objects)
    never calls __init__. You have to override __new__ instead:

    py> class Point3D(tuple):
    .... def __new__(cls, x, y, z):
    .... obj = super(Point3D, cls).__new__(cls, (x,y,z))
    .... return obj
    ....
    py> a = Point3D(10, 20, 30)
    py> a
    (10, 20, 30)
    py> type(a)
    <class '__main__.Point3D'>

    See http://docs.python.org/reference/datamodel.html#basic-customization

    --
    Gabriel Genellina
     
    Gabriel Genellina, Aug 15, 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. pantalaimon
    Replies:
    3
    Views:
    51,084
    John Harrison
    Oct 9, 2004
  2. Ben Kial
    Replies:
    1
    Views:
    669
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,902
    Eric Sosman
    Jan 7, 2005
  4. Replies:
    6
    Views:
    511
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,253
Loading...

Share This Page