Polymorphism using constructors
Diez B. Roggisch
deets at nospam.web.de
Mon Mar 3 15:09:01 EST 2008
K Viltersten schrieb:
> I'm writing a class for rational numbers
> and besides the most obvious constructor
>
> def __init__ (self, nomin, denom):
>
> i also wish to have two supporting ones
>
> def __init__ (self, integ):
> self.__init__ (integ, 1)
> def __init__ (self):
> self.__init__ (0, 1)
>
> but for some reason (not known to me at
> this point) i get errors. My suspicion is that it's a syntax issue.
>
> Suggestions?
"errors" is not much of an error-description. That's what stacktraces
are for.
Apart from that, you won't succeed with the above. Python has no
signature-based polymorphism. Instead, you use default arguments, like this:
def __init__(nomin=0, denom=1):
...
That should suffice.
Diez
More information about the Python-list
mailing list