[Tutor] Contructor Overloading and Function Tooktips

Brian van den Broek bvande at po-box.mcgill.ca
Tue Apr 19 20:29:49 CEST 2005


Gooch, John said unto the world upon 2005-04-19 10:20:
> Brian, 
> 
> I think in the OO world it is called Polymorphism, 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.
> 
> Here are some example function declarations ( C++ style )
> boolean greaterThan( int A, int B );
> boolean greaterThan( char A, char B );
> boolean greaterThan( double A, double B );
> 
> All of these functions are called as "greaterThan( A,B )" and all of them
> return a boolean "true" or "false" value, but the compiler decides which one
> of the three functions above gets called depending on the data type of A and
> 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:
> 
> class Circle : Shape {
> public:
> 	Circle();//creates default circle object
> 
> 	Circle( int x, int y, float radius ); //creates circle object with
> specified x,y coordinates and radius
> };
> 
> 
> now, replace 'Circle' in the 'public:' area with '__init__' and you have a
> picture of what I would like to do in Python, but I don't know if the
> language support it or not. 
> 
> Thank you for answering the second part of the question, I will try that
> technique out.
> 

Hi John,

OK, I think I see what you meant.

First thing is, while I understand it was just an example, your 
greaterThan examples are handled differently in Python, I think. There 
are "special methods" for stipulating how the objects of a class 
behave when passed to the standard comparisons (like '>'). They are 
all methods with double leading and trailing underscores such as 
__gt__ I'm typing off line, but searching the docs for "special 
method" should get you there. (Also, if you have it, p.92 of Python in 
a Nutshell covers them.)

OK, but that was just your example :-)  Here is some ugly quick code 
which might show you how to meet you needs:

class JohnsMultiInitClass:
     def __init__(self, first, second):
         if type(first) == type(second) == str:
             self.string_init(first, second)
         if type(first) == type(second) == int:
             self.int_init(first, second)
     def string_init(self, first, second):
         # string init code here
     def int_init(self, first, second):
         # int init code here

That said, I'm not so sure this is a good thing. I'm no pro, still 
getting an handle on OOP, and dangerous only in Python. But, I suspect 
that this is not the Python way to do this sort of thing. (Perhaps 
people with C++ experience can weigh in to confirm or deny.)

Best,

Brian vdB



More information about the Tutor mailing list