Classes Can Intercept Python Operators : Operator Overloading « Class « Python Tutorial






class FirstClass:                  
    def setdata(self, value):      
        self.data = value          
    def display(self): 
        print self.data            



class SecondClass(FirstClass):     
    def display(self):             
        print 'Current value = "%s"' % self.data 


class ThirdClass(SecondClass):     
    def __init__(self, value):     
        self.data = value 
    def __add__(self, other):      
        return ThirdClass(self.data + other) 
    def __mul__(self, other): 
        self.data = self.data * other 
a = ThirdClass("abc")           
a.display(  )                   
b = a + 'xyz'                   
b.display(  ) 

a * 3                           
a.display(  )








11.5.Operator Overloading
11.5.1.Operator Overloading: add, sub, mul, div
11.5.2.Operator Overloading: sub
11.5.3.Classes Can Intercept Python Operators