Overloaded Constructor
Jeff Kunce
kuncej at mail.conservation.state.mo.us
Wed Oct 18 11:21:40 EDT 2000
> How can I use overloaded constructor in Python?
> Such as
>
> class myClass:
> def __init__(self):
> dosomething()
> def __init__(self,any_var1,any_var2):
> dosomething_with_any_varx()
>
You could use multiple "constructor functions" external to the class:
class myClass:
def __init__(self):
dosomething_that_always_must_be_done()
def myClassA(self):
mc = myClass()
mc.dosomething()
return mc
def myClassB(any_var1,any_var2)
mc = myClass()
mc.dosomething_with_any_varx(any_var1,any_var2)
return mc
If you combine this approach with optional/keyword arguments
in __init() (as suggested by others), you can provide a lot of options.
--Jeff
More information about the Python-list
mailing list