[Tutor] Contructor Overloading and Function Tooktips
Danny Yoo
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?
Hi John,
In general, Python doesn't support "overloading". Overloading wouldn't
interact well at all with functions that can take a variable number of
arguments.
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.
For example:
######
>>> 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
mailing list