polymorphism in python

Roy Smith roy at panix.com
Wed Nov 26 22:53:36 CET 2003


Jay O'Connor <joconnor at cybermesa.com> wrote:
> Languages like Python [and] Smalltalk [...] are not as good at
> overloading based on argument types (how do you have two methods
> that have the same name but one takes a single integer argument
> and one takes a single string argument?

But how often is it actually useful to do such a thing?  I can't 
remember the last time I wanted to do something like that in Python.  
Did lots of it in C++, but that's more working around the language than 
anything else.  Can you think of a good example of why you would want to 
do something like that in Python?

If your intent is that you could call foo(4) or foo('4'), you could just 
write:

def foo (x):
   theRealX = int (x)

or you could maybe even do:

def foo (x):
   if type(x) == StringType:
      do string stuff
   else:
      do integer stuff

but if you're trying to do either of the above, I suspect you're trying 
to write C++ in Python (which is as bad, if not worse, as trying to 
write Fortran in Python).




More information about the Python-list mailing list