In addition to working with Python, I also work with Java quite a lot.
When coding in Python, I occasionally encounter situations in which I wish I could code multiple constructors (with different signatures) for a class, the way you can in Java.
Recently, someone else had the same desire, and posted his question on comp.lang.python. So I thought that I would post an example of the technique that I use, in case others might find it useful. So here it is:
import sys, types, pprint class Vector: """ Demo of a class with multiple signatures for the constructor """ def __init__(self, *args, **kwargs): if len(args) == 1: foundOneArg = True; theOnlyArg = args else: foundOneArg = False; theOnlyArg = None if foundOneArg and isinstance(theOnlyArg, types.ListType): self.initializeFromList(theOnlyArg) elif foundOneArg and isinstance(theOnlyArg,Vector): self.initializeFromVector(theOnlyArg) else: self.initializeFromArgs(*args) pprint.pprint(self.values) # for debugging only def initializeFromList(self, argList): self.values = [x for x in argList] def initializeFromVector(self, vector): self.values = [x for x in vector.values] def initializeFromArgs(self, *args): self.values = [x for x in args] #------------ end of class definition --------------------- v = Vector(1,2,3) v = Vector([4,5,6]) q = Vector(v);