Inherited method

class FirstClass:
def setdata(self, value):
self.data = value # self is the instance
def display(self):
print self.data # self.data: per instance
class SecondClass(FirstClass): # inherits setdata
def display(self): # changes display
print 'Current value = "%s"' % self.data
class ThirdClass(SecondClass):
def __init__(self, value):
self.data = value
def __add__(self, other): # on "self + other"
return ThirdClass(self.data + other)
def __mul__(self, other):
self.data = self.data * other # on "self * other"
a = ThirdClass("abc") # new __init__ called
a.display() # inherited method
b = a + 'xyz' # new __add__: makes a new instance
b.display()
a * 3 # new __mul__: changes instance in-place
a.display()
Related examples in the same category