Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Calling parent constructor with different argument list

Reply
Thread Tools

Calling parent constructor with different argument list

 
 
pinkisntwell
Guest
Posts: n/a
 
      08-14-2009
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?
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      08-14-2009
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.

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-15-2009
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/dat...-customization

--
Gabriel Genellina

 
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
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
Calling parent constructor from child constructor pantalaimon C++ 3 10-09-2004 09:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57