[Tutor] Contructor Overloading and Function Tooktips
dyoo at hkn.eecs.berkeley.edu
Sat Apr 16 03:31:45 CEST 2005
On Fri, 15 Apr 2005, Gooch, John wrote:
> Is there a way to create multiple __init__ routines in a Python Class?
In general, Python doesn't support "overloading". Overloading wouldn't
interact well at all with functions that can take a variable number of
That being said, can you write a really general initializer that fits all
your cases? If so, then you might provide a set of utility factory
functions to simplify calling the general initializer.
>>> class BankAccount:
... def __init__(self, name, initialAmount):
... self.name = name
... self.amount = initialAmount
>>> def makeBankruptAccount(name):
... return BankAccount(name, 0)
>>> def makeMillionareAccount(name):
... return BankAccount(name, 10 ** 6)
>>> def makeAnonymousDonor(amount):
... return BankAccount(None, amount)
Best of wishes!
More information about the Tutor