Multiple initialization methods?
Dennis Benzinger
Dennis.Benzinger at gmx.net
Wed Feb 16 16:52:16 EST 2005
alex wrote:
> Hi,
>
> it is possible to define multiple initialization methods so that the
> method is used that fits?
No, there is no overloading in Python.
> I am thinking of something like this:
>
> def __init__(self, par1, par2):
> self.init(par1, par2);
>
> def __init__(self, par1):
> self.init(par1, None)
>
> def init(self, par1, par2):
> ...
> ...
>
> So if the call is with one parameter only the second class is executed
> (calling the 'init' method with the second parameter set to 'None' or
> whatever. But this example does not work.
>
> How to get it work?
>
> Alex
>
Use a default argument for par2 and check for that in your function:
def __init__(self, par1, par2=None):
# do something with par1
if par2 is None:
print "par2 was not given!"
else:
print "par2 is", par2
Read more in the FAQ:
http://www.python.org/doc/faq/programming.html#how-can-i-overload-constructors-or-methods-in-python
or in the tutorial:
http://docs.python.org/tut/node6.html#SECTION006710000000000000000
Bye,
Dennis
More information about the Python-list
mailing list