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

Jeffrey Maitland jeff.maitland at gmail.com
Wed Jun 22 15:48:06 EDT 2005


Well one way to do this (not sure if it is the best way) is something like.

class mypoint:
   def __init__(self, *args):
      len_args = len(args)
      print len_args
      if len_args == 0:
          self.x = 0
          self.y = 0
          self.z = 0
      elif len_args >=2 and len_args <= 3:
          for i in range(len_args):
              if args[i] == None: args[i] = 0 #populating the list of
args with defgault values if null(None)
          self.x = kargs[0]
          self.y = kargs[1]
          if len_args == 3: self.z = args[2]
      else: raise "Invalid Input"

Now this is not looking for null input such as 
p = mypointclass(,,,)
this is an invalid syntax error.  Now there must be away to catch for
the blanks  but Iam not sure off the top of my head.
 hope that helps
 

On 6/22/05, scott <scott at alussinan.org> 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
> #--
> 
> tia people
> scott
> 
> *this is not homework
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list