function overloading
Jay O'Connor
joconnor at cybermesa.com
Sat May 24 12:07:51 EDT 2003
On Sat, 24 May 2003 17:33:10 +0200, Mirko Koenig <koenig at v-i-t.de>
wrote:
>Is it not possible in python to overload fucntion with the same name?
>How do you do it?
Function overloading tends not to work in dynamically bound languages.
In C you can have
int myFunc (int a) {};
int myFunc (char* a) {};
and the compiler can tell by the types involved which function to
call.
languages that don't statically bind variables to a given type don't
do so well with this:
def myFunc (myInt):
pass
def myFunc (myString):
pass
Since the function definiftion has no type information, the compiler
could not resolve which function to call
In Pyhon's case, the functions are kept in a dictionary keyed of the
function name (iirc) so having two functions with the same name but
different parameter lists doesn't work either. When the compiler
compiles the second function, it replaces the first in the dictionary,
which is why your last function is the only one that gets called.
You are better off either
a) having functions with slightly different names or
b) using polymorphism to allow one function to handle different types,
cleanly.
Take care,
Jay
More information about the Python-list
mailing list