This is a discussion on Does Python supports Operator Overloading? within the Python forums, part of the Software Development category; Does Python supports Operator Overloading?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Does Python supports Operator Overloading? |
|
#2
| |||
| |||
| Hi, Operator Overloading in Python A simple class with operator overloading example in Python. __add__(self,other) overloads the + symbol when an instance of addSquare is on the left. __radd_ _ overloads the + symbol when the instance is on the right. The __add__ method checks the other object being added, and if it's a string, it converts it to a floating point. The class adds the squares of the two objects. (the "__" denotes special methods). import string class addSquare: def __init__(self,value=1): self.value = value def __add__(self,other): if type(other) == type("abc"): other = string.atof(other) return self.value**2 + other**2 __radd__ = __add__ -R.Gopi |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Difference between function overloading and operator overloading | vigneshgets | C and C++ Programming | 6 | 12-17-2008 06:21 PM |
| environments supports | simplesabita | Testing Tools | 1 | 08-22-2007 02:59 AM |
| Does Python supports Function Overloading? | Arun | Python | 2 | 08-14-2007 04:46 AM |
| Is there any Increment/Decrement operator in Python? | vadivelanvaidyanathan | Python | 0 | 07-16-2007 10:16 PM |
| Operator overloading in C++ | prasath | C and C++ Programming | 1 | 07-16-2007 05:51 AM |
Our Partners |