constructor overwrite
Arnaud Delobelle
arnodel at googlemail.com
Mon Feb 15 09:41:53 EST 2010
Mug <exallion.long at gmail.com> writes:
> hi ,i had a problem on constructor overwrite:
> i have something like:
>
> class obj:
> def __init__(self, x=100, y=None):
> if y is None:
> self.x=x
> else:
> self.y=y
> so i can call :
> objet = obj() # x=100 y=None
> or
> objet = obj(40) # x= 40 y=None
>
> but if i do :
> objet = obj('not cool') #x='not cool' y=None
> since x is not typed .
>
> i am waiting for a result:
> objet = obj('not cool') #x=100 y='not cool'
> as they do in C++ or java.
> is there a way to do it?
> thanks
Your problem doesn't seem very well defined (e.g. do you ever call obj
with two arguments?), but as I understand it you can do this:
def __init__(self, x=100):
if isinstance(x, int):
self.x, self.y = x, None
else:
self.x, self.y = None, x
HTH
--
Arnaud
More information about the Python-list
mailing list