IT Community - Software Programming, Web Development and Technical Support

Does Python supports Operator Overloading?

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?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Python

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 07-16-2007, 10:15 PM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 801
vadivelanvaidyanathan is on a distinguished road
Question Does Python supports Operator Overloading?

Does Python supports Operator Overloading?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 07-17-2007, 02:10 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 119
Gopisoft is on a distinguished road
Default Re: Does Python supports Operator Overloading?

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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


All times are GMT -7. The time now is 06:16 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0