[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Polymorphism using constructors

K Viltersten

3/3/2008 8:21:00 PM

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?

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

4 Answers

Raymond Hettinger

3/3/2008 9:17:00 PM

0

On Mar 3, 12:21 pm, "K Viltersten" <t...@viltersten.com> wrote:
> 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)

For this particular use case, providing default arguments will
suffice:

class Fraction:
def __init__(self, numerator=0, denomiator=1):
...

Since Python doesn't support having two methods with the same name,
the usual solution is to provide alternative constructors using
classmethod():

@classmethod
def from_decimal(cls, d)
sign, digits, exp = d.as_tuple()
digits = int(''.join(map(str, digits)))
if sign:
digits = -digits
if exp >= 0:
return cls(digits * 10 ** exp)
return cls(digits, 10 ** -exp)


Raymond

Carl Banks

3/3/2008 10:04:00 PM

0

On Mar 3, 4:17 pm, Raymond Hettinger <pyt...@rcn.com> wrote:
> Since Python doesn't support having two methods with the same name,
> the usual solution is to provide alternative constructors using
> classmethod():
>
> @classmethod
> def from_decimal(cls, d)
> sign, digits, exp = d.as_tuple()
> digits = int(''.join(map(str, digits)))
> if sign:
> digits = -digits
> if exp >= 0:
> return cls(digits * 10 ** exp)
> return cls(digits, 10 ** -exp)


Note that even some of Python's built in types (dict *cough*)
implement homemade function overloading.

The OP wanted to write a constructor that could accept either a pair
of integers or a rational, there would be a good precedent for it.

However, I would advise the OP to use the constructor only for the
most common arguments, and use classmethods for more obscure, less
common arguments (such as decimal or even float).


Carl Banks

Aaron Brady

3/4/2008 3:21:00 AM

0

On Mar 3, 4:03 pm, Carl Banks <pavlovevide...@gmail.com> wrote:
> On Mar 3, 4:17 pm, Raymond Hettinger <pyt...@rcn.com> wrote:
>
> > Since Python doesn't support having two methods with the same name,
> > the usual solution is to provide alternative constructors using
> > classmethod():
>
> >   @classmethod
> >   def from_decimal(cls, d)
> >         sign, digits, exp = d.as_tuple()
> >         digits = int(''.join(map(str, digits)))
> >         if sign:
> >             digits = -digits
> >         if exp >= 0:
> >             return cls(digits * 10 ** exp)
> >         return cls(digits, 10 ** -exp)
>
> Note that even some of Python's built in types (dict *cough*)
> implement homemade function overloading.
>
> The OP wanted to write a constructor that could accept either a pair
> of integers or a rational, there would be a good precedent for it.
>
> However, I would advise the OP to use the constructor only for the
> most common arguments, and use classmethods for more obscure, less
> common arguments (such as decimal or even float).
>
> Carl Banks

Unless application indicates calling signature uniformity, hash type
to subclass or to class method.

K Viltersten

3/4/2008 8:00:00 PM

0

"Carl Banks" <pavlovevidence@gmail.com> skrev i meddelandet
news:24adaf72-0c55-4a88-86d0-296363ead17f@s8g2000prg.googlegroups.com...
> On Mar 3, 4:17 pm, Raymond Hettinger <pyt...@rcn.com> wrote:
>> Since Python doesn't support having two methods with the same name,
>> the usual solution is to provide alternative constructors using
>> classmethod():
>>
>> @classmethod
>> def from_decimal(cls, d)
>> sign, digits, exp = d.as_tuple()
>> digits = int(''.join(map(str, digits)))
>> if sign:
>> digits = -digits
>> if exp >= 0:
>> return cls(digits * 10 ** exp)
>> return cls(digits, 10 ** -exp)
>
>
> Note that even some of Python's built in types (dict *cough*)
> implement homemade function overloading.
>
> The OP wanted to write a constructor that could accept either a pair
> of integers or a rational, there would be a good precedent for it.
>
> However, I would advise the OP to use the constructor only for the
> most common arguments, and use classmethods for more obscure, less
> common arguments (such as decimal or even float).


OP understands and thanfully accepts
the suggestion.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy