Overload __mul__ method for multiply operator

Overload __mul__


class FirstClass:                  
    def setdata(self, value):      
        self.data = value          #  ww  w.  ja v  a 2  s.  c om
    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(  ) 

The code above generates the following result.





















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules