Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Python Class Inheritance

#!/usr/bin/python

class Parent:        # define parent class
   parentAttr = 100
   def __init__(self):
      print "Calling parent constructor"

   def parentMethod(self):
      print 'Calling parent method'

   def setAttr(self, attr):
      Parent.parentAttr = attr

   def getAttr(self):
      print "Parent attribute :", Parent.parentAttr
      
class Parent2:
    parentAttr = 300
    
    def parentMethod(self):
        print "Calling parent2 method"
        
    def setAttr(self, attr):
        Parent2.parentAttr = attr
        
    def getAttr(self):
        print Parent2.parentAttr

class Child(Parent,Parent2): # define child class
   def __init__(self):
      print "Calling child constructor"

   def childMethod(self):
      print 'Calling child method'

c = Child()          # instance of child
c.childMethod()      # child calls its method
c.parentMethod()     # calls parent's method
#c.setAttr(200)       # again call parent's method
c.getAttr()          # again call parent's method

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.