You cannot have two constructors in the same class.
Constructors should be named __init__ . And, unlike Java, Python does not allow overloading functions or methods by the type of their arguments. So, if you had two constructors, they would both be the same function.
There are several ways around this.
Use @classmethod as alternative constructors:
class Breakfast(object): @classmethod def from_eggs(cls, eggs): obj = cls() obj.spam, obj.eggs = 5, eggs return obj @classmethod def from_spam_and_eggs(cls, spam, eggs): obj = cls() obj.spam, obj.eggs = spam, eggs return obj
A simple example from the standard library is datetime.datetime , which can be built using now , fromtimestamp or several other alternative constructors besides the default .
Use the default parameters, keywords and / or variable arguments to create one constructor, which can be called in different ways:
class Breakfast(object): def __init__(self, eggs=0, spam=5): self.spam, self.eggs = spam, eggs
int is an example of this: you can create it from a string and a base, or from a single argument that knows how to convert itself to an integer.
Create subclasses, each of which has different constructors:
class Breakfast(object): pass class HealthyBreakfast(object): def __init__(self, spam): self.spam, self.eggs = spam, 0 class NormalBreakfast(object): def __init__(self, spam, eggs): self.spam, self.eggs = spam, eggs
In either of these cases, you can split the commonalities into one “base” initializer. For example:
class Breakfast(object): def __init__(self, eggs, spam): self.spam, self.eggs = spam, eggs class HealthyBreakfast(object): def __init__(self, spam): super(HealthyBreakfast, self).__init__(0, spam)
Of course, in no case should you eat breakfast without spam.