Inheritance


Classes can inherit functionality of other classes.

Video: Inheritance

Intoduction

We define a basic class named User:

class User:
    name = ""
 
    def __init__(self, name):
        self.name = name
 
    def printName(self):
        print "Name  = " + self.name
 
brian = User("brian")
brian.printName()

This creates one instance called brian which outputs its given name. We create another class called Programmer.

class Programmer(User):
 
    def __init__(self, name):
        self.name = name
    def doPython(self):
        print "Programming Python"

This looks very much like a standard class except than User is given in the parameters. This means all functionality of the class User is accessible in the Programmer class.

Inheritance example

Full example of Python inheritance:

class User:
    name = ""
 
    def __init__(self, name):
        self.name = name
 
    def printName(self):
        print "Name  = " + self.name
 
class Programmer(User):
    def __init__(self, name):
        self.name = name
 
    def doPython(self):
        print "Programming Python"
 
brian = User("brian")
brian.printName()
 
diana = Programmer("Diana")
diana.printName()
diana.doPython()

The output:

Name  = brian
Name  = Diana
Programming Python

Brian is an instance of User and can only access the method printName. Diana is an instance of Programmer, a class with inheritance from User, and can access both the methods in Programmer and User.


22 thoughts on “Inheritance

  1. Manuel Giron - August 17, 2015

    Hi!, I have a question, how can I declare X number of variables in the Programmer Class, and set them when getting the instance of it. for example I want to set the programming language in the Programmer Class extending all the other parameters of the User Class.

    1. Frank - August 17, 2015

      The traditional way is to use setter functions, in this example we have the variable name from the user class while also having language from the programmer class:

       
      class User:
          name = ""
       
          def __init__(self, name):
              self.name = name
       
          def printName(self):
              print "Name = " + self.name
       
      class Programmer(User):
       
          language = ""
       
          def setLanguage(self, language):
              self.language = language
       
          def printLanguage(self):
              print self.language
       
      manuel = Programmer("Manuel")
      manuel.setLanguage("Python")
      manuel.printName()
      manuel.printLanguage()

      A more detailed example, where variables from the User class and Programmer class are used:

      class User:
          name = None
          job = None
       
          def __init__(self, name, job):
              self.name = name
              self.job = job
       
          def printName(self):
              print "Name = " + self.name
       
          def printJob(self):
              print "Job = " + self.job
       
      class Programmer(User):
       
          language = ""
       
          def setLanguage(self, language):
              self.language = language
       
          def printLanguage(self):
              print "Language = " + self.language
       
      guido = Programmer("Guido","Developer")
      guido.setLanguage("Python")
      guido.printName()
      guido.printLanguage()
      guido.printJob()

      An instance can be created only from one class, thus either the Programmer class or User class. If we overwrite the User constructor, we can no longer access its variables, thus we have to pick one constructor. If we pick the Programmers constructor we would have:

      class User:
          name = None
          job = None
       
          def setName(self,name):
              self.name = name
       
          def setJob(self,job):
              self.job = job
       
          def printName(self):
              print "Name = " + self.name
       
          def printJob(self):
              print "Job = " + self.job
       
      class Programmer(User):
       
          language = ""
       
          def __init__(self, language):
              self.language = language
       
          def printLanguage(self):
              print "Language = " + self.language
       
      guido = Programmer("Python")
      guido.setName("Guido")
      guido.setJob("Developer")
      guido.printName()
      guido.printLanguage()
      guido.printJob()

      To set multiple variables you can either pass a list/tuple to an setter function or define various setter functions and call them one by one. I hope that helps, let me know if you have any questions

  2. Sheik - August 2, 2015

    Hi Frank,

    My Question related to one of the previous question by a friend ‘Anna Gao’.
    What is the difference between ‘import user’ and ‘from user import *’?

    I am getting a following error when I create a sub class with string parameter (even though the base class also of same type) with ‘import user’ but works fine with ‘from user import *’

    NameError: name ‘User’ is not defined

    1. Frank - August 2, 2015

      ‘imports X’ imports the module. You can use X.name to access the variables.
      ‘from X import *’ imports the module X you can access all publics directly.

  3. Siva - July 31, 2015

    why do we have to define again the constructor in the inherited class Programmer??

    1. Frank - July 31, 2015

      This is optional. You may want to create a new Programmer object which inherits from another class, but have specific behavior in the constructor.

  4. Chhavleen Singh - July 30, 2015

    How many types of Inheritance is supported by Python ? Is there any feature of abstract class or interface in Python ? I am from JAVA background. Thanks.

    1. Frank - July 31, 2015

      It depends on your version. Python version 2.6 and later have Abstract Base Classes.
      Unlike Java, Python supports multiple inheritance:

      #!/usr/bin/env python
       
      class ClassA( object ):
          def sayA( self ):
              print('say A')
       
      class ClassB( object ):
          def sayB( self ):
              print('say B')
       
      class Concrete( ClassA, ClassB ):
          pass
       
       
      c = Concrete()
      c.sayA()
      c.sayB()