Inherited method : Class Inheritance : Class : Python examples (example source code) Organized by topic

C++
PHP
Python
Python Home »  Class   » [  Class Inheritance  ]  Screenshots 
 



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
1.  Use __class__, __bases__ and __dict__ for sub and super class Use __class__, __bases__ and __dict__ for sub and super class
2.  Print out class tree Print out class tree
3.  Class inherited Class inherited
4.  Class inheritance: inherit member variable override function Class inheritance: inherit member variable override function
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.