[Tutor] Overloading + factoring

Ivan Van Laningham ivanlan at callware.com
Sat Dec 4 11:32:46 EST 1999


Hi All--

FFlores wrote:
> 

[snip]

> Well, that's another thing! I was referring to C++-like
> overloading (i. e. several definitions of the same method,
> to be tested in order until the parameters fit). But I've
> already been told that's not possible.

No, it's not possible, but you don't really need it.  You can use the
special class methods, such as __add__ and __radd__, and those will
cover a larger spectrum than C++ does for the + operator.  That is, with
C++ you can define methods such that <instance> + 1 means something, but
you can't define a method for 1 + <string> as you can with Python's
__radd__ method.  Inside each such method, just check the types of the
other operand.  If it's an int, do thing a, for a long, thing b, and so
on.  I submit that you'll end up with less code that is far more
comprehensible doing it this way in Python than you can ever hope to
achieve in C++.

[snip]

> > > And something else, though it's not Python-related:
> > > is there a nice method for factoring numbers, calculating
> > > lcd, gcd, and/or a good library of such functions for rational
> > > numbers?
> >
> > Not to my knowledge.  But, you could probably write one yourself.
> 
> Oh yes, I could make a function that gives me the prime numbers
> I need. But I'd become old and die while the interpreter is still
> calculating. :) Thanks anyway.
> 

I think Tim Peters has a rational number library.  I posted a gcd()
function on the list awhile back, taken from Knuth's extended gcd
example, which Tim then proceeded to improve greatly.  Deja news lets
you search the archives, but I also think that the exgcd() function got
snapped up for the snippets website.

Sorry I don't have the URLs for either Deja news or the snippets. 
Shamefully, I don't even remember who is responsible for the snippets
site.  I bet parnassus would have pointer to the snippets site, and
might even have one to Tim's rationals.

<you-don't-have-to-grow-old-and-die-if-your-input-domain-is-small-enough>-ly
y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours
----------------------------------------------




More information about the Python-list mailing list