Overloading

jepler at unpythonic.net jepler at unpythonic.net
Fri Dec 9 12:47:37 EST 2005


On Fri, Dec 09, 2005 at 06:29:12PM +0100, Johannes Reichel wrote:
> Hi!
> 
> In C++ you can overload functions and constructors. For example if I have a
> class that represents a complex number, than it would be nice if I can
> write two seperate constructors

Python doesn't support this, but it does support default arguments:
    class Complex:
        def __init__(self, real=0, imag=0):
            self.real = real
            self.imag = imag

> And by the way, is it possible to overload operators like +,-,*?
> 
> def operator+(self,complex2):

The special methods have names like __add__.
    http://docs.python.org/ref/numeric-types.html

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20051209/6d92c240/attachment.sig>


More information about the Python-list mailing list