Using Multiple Constructors
Skip Montanaro
skip at pobox.com
Fri Aug 3 17:42:12 EDT 2001
Mikhael> Class MyObject:
Mikhael> Constructor1(self, number):
Mikhael> pass
Mikhael> Constructor2(self, string):
Mikhael> pass
You can only have one constructor. Since you can't declare Python object
types, you have to check them at runtime. One way you might find useful is
to force your users to use keyword args:
class MyObject:
def __init__(self, **kwds):
if kwds.has_key("number"):
self.number_init(kwds["number"])
elif kwds.has_key("string"):
self.string_init(kwds["number"])
There are other ways to break the problem up as well, including providing
various defaults:
class MyObject:
def __init__(self, number=None, string=None):
...
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list