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

Michael Hoffman cam.ac.uk at mh391.invalid
Wed Jun 22 13:24:58 EDT 2005


scott wrote:

> 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
> #--

Well for the example you used, multiple constructors are not needed. 
This will get you the same result as what I imagine you wanted the
first example to do:

class myPointClass:
     def __init__(self, x=0, y=0, z=0):
          self.x = x
          self.y = y
          self.z = z
-- 
Michael Hoffman



More information about the Python-list mailing list