how to use more than 1 __init__ constructor in a class ?

Roy Smith roy at
Wed Jun 22 21:39:30 CEST 2005

scott  <scott at> wrote:
>hi people,
>can someone tell me, how to use a class like that* (or "simulate" more 
>than 1 constructor) :
>class myPointClass:
>   def __init__(self, x=0, y=0):
>     self.x = x
>     self.y = y
>   def __init__(self, x=0, y=0, z=0):
>     self.__init__(self, x, y)
>     self.z = z

Python does not have the kind of polymorphism that C++ or Java has.
There is only a single copy of each method (including __init__) for
each class, but methods can take variable numbers of arguments, with
default values.  You probably want something like:

def __init__(self, x=0, y=0, z=0):
    self.x = x
    self.y = y
    self.z = z

You can call this with 0, 1, or 2 arguments, i.e. any of the following
are legal:

MyClass()        # x, y, and z all get defaulted to 0
MyClass(1)       # x=1, y and z both default to 0
MyClass(1, 2)    # x=1, y=2, z defaults to 0
MyClass(1, 2, 3) # x=1, y=2, z=3

Once you get the hang of it, you'll understand just how brain-dead C++
and Java really are :-)

Take a look at for more

More information about the Python-list mailing list