[Tutor] Contructor Overloading and Function Tooktips

Alan Gauld alan.gauld at freenet.co.uk
Tue Apr 19 19:50:49 CEST 2005


> I think in the OO world it is called Polymorphism,

Nope, its called overloading. Polymorphism is where you
send the same message to diferent types of object and
get different behaviour.

> where you have a single function name, but multiple definitions
> that are distinguished from one another by the number of arguments,
> type of arguments, and sometimes ( Smalltalk ) the return type
> of the function.

Actually the return type in Smalltalk is always an object
so its irrelevant, however what you say is true of
Objective C as an example.

> B. My first question was whether or not you can do this in Python
with the
> __init__ function. In C++ you can have multiple contructors for a
class,
> with the arguments deciding which contructor is called. Here is an
example:

The answer is no, but you can examoine the arguments at run time
and call any one of several helper functions from within init()
which achieves the same effect.

Python cannot easily support this feature because it relies on
binding of type to name but in Python names are simply keys in
a dictionary, the object asociated can be of any type and even
change type over time. THuds we must rely on runtime introspection
of the type of the object to decide which version of "init()"
we need.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list