Inheritance in Python | Set 2
Last Updated :
31 Aug, 2018
Prerequisite :
basics of inheritance in Python,
Inheritance, examples of object, issubclass and super
There are 2 built-in functions in Python that are related to inheritance. They are:
1. isinstance(): It checks the type of an object. Its syntax is:
isinstance(object_name, class_name)
It would return
True if the class of object_name is class_name else
False.
For example:
Python3 1==
# Python code to demonstrate issubclass()
print(isinstance(5, int))
The above code would show the following output:
True
This is because 5 is an integer and hence belongs to the class of int.
NOTE: 'int' is both a type and a class in Python.
2. issubclass(): It checks whether a specific class is the child class of another class or not. Its syntax is:
issubclass(childclass_name, parentclass_name)
It would return
True if the entered child class is actually derived from the entered parent class else, it returns
False.
For example:
Python3 1==
# Python code to demonstrate issubclass()
class A():
def __init__(self, a):
self.a = a
class B(A):
def __init__(self, a, b):
self.b = b
A.__init__(self, a)
print(issubclass(B, A))
The above code would show the following output:
True
Multiple Inheritance:
When a class inherits from more than one parent class, it is called as multiple inheritance. It works in the same way as single inheritance.
Python 1==
# Python code to demonstrate multiple inheritance
# first parent class
class Person(object):
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
# second parent class
class Employee(object):
def __init__(self, salary, post):
self.salary = salary
self.post = post
# inheritance from both the parent classes
class Leader(Person, Employee):
def __init__(self, name, idnumber, salary, post, points):
self.points = points
Person.__init__(self, name, idnumber)
Employee.__init__(self, salary, post)
While creating an instance of a class, make sure that the sequence in which you give the parameters to a function is relevant to that within the block of the class. For example, in the above code, if we have to generate an instance, we would write
ins = Leader('Rahul', 882016, 75000, 'Assistant Manager', 560)
If you interchange the sequence position of 75000 and 'Assistant Manager', the code would take 'Assistant Manager' as the salary and 75000 as the post.
See for yourself.
Python3 1==
# first parent class
class Person(object):
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
# second parent class
class Employee(object):
def __init__(self, salary, post):
self.salary = salary
self.post = post
# inheritance from both the parent classes
class Leader(Person, Employee):
def __init__(self, name, idnumber, salary, post, points):
self.points = points
Person.__init__(self, name, idnumber)
Employee.__init__(self, salary, post)
print(self.salary)
ins = Leader('Rahul', 882016, 'Assistant Manager', 75000, 560)
Output:
Assistant Manager
Overriding Methods:
Overriding a method means redefining a method in the subclass when it has already been defined in some other class.
A method in the subclass would be called as overridden only when there exists another method with the same name and same set of parameters in the superclass.
Also, we cannot override a private method of a superclass, which is the one having double underscores before its name.
For example:
Python 1==
# Base Class
class A(object):
def __init__(self):
constant1 = 1
def method1(self):
print('method1 of class A')
class B(A):
def __init__(self):
constant2 = 2
self.calling1()
A. __init__(self)
def method1(self):
print('method1 of class B')
def calling1(self):
self.method1()
A.method1(self)
b = B()
Output:
method1 of class B
method1 of class A
The code invokes the method1 of class B and not A because Python searches for the function in the bottom to top order.
If you want to invoke the method1 of class A, replace self.method1() with A.method1(self).
The above procedure for overriding methods works in old-style classes, which are the classes where the parent class doesn't inherit from the 'object' class.
For new-style classes, where the parent class inherits from the built-in 'object' class, there is another procedure for overriding methods.
The super() method helps us in overriding methods in new style classes. Its syntax is as follows:
super(class_name, instance_)of_class).overridden_method_name()
Let us assume there are 3 classes A, B, and C. All 3 of them have a common function called 'method1'. Here comes the work of super().
Python 1==
class A(object):
def function1(self):
print 'function of class A'
class B(A):
def function1(self):
print 'function of class B'
super(B, self).function1()
class C(B):
def function1(self):
print 'function of class C'
super(C, self).function1()
j = C()
j.function1()
Output:
function of class C
function of class B
function of class A
The 'self' parameter within super function acts as the object of the parent class and hence invokes the function1 of the parent class.
Similar Reads
Inheritance in Python
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure. In this arti
7 min read
Python | super() in single inheritance
Prerequisites: Inheritance, function overriding At a fairly abstract level, super() provides the access to those methods of the super-class (parent class) which have been overridden in a sub-class (child class) that inherits from it. Consider the code example given below, here we have a class named
6 min read
Data Classes in Python | Set 4 (Inheritance)
Prerequisites: Inheritance In Python, Data Classes in Python | Set 3 In this post, we will discuss how DataClasses behave when inherited. Though they make their own constructors, DataClasses behave pretty much the same way as normal classes do when inherited. Python3 1== from dataclasses import data
2 min read
What Is Hybrid Inheritance In Python?
Inheritance is a fundamental concept in object-oriented programming (OOP) where a class can inherit attributes and methods from another class. Hybrid inheritance is a combination of more than one type of inheritance. In this article, we will learn about hybrid inheritance in Python. Hybrid Inheritan
3 min read
Multiple Inheritance in Python
Inheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P. Multiple Inheritance When a class
5 min read
Types of inheritance Python
Inheritance is defined as the mechanism of inheriting the properties of the base class to the child class. Here we a going to see the types of inheritance in Python. Types of Inheritance in Python Types of Inheritance depend upon the number of child and parent classes involved. There are four types
3 min read
Conditional Inheritance in Python
It happens most of the time that given a condition we need to decide whether a particular class should inherit a class or not, for example given a person, if he/she is eligible for an admission in a university only then they should be a student otherwise they should not be a student. Let's consider
3 min read
Inheritance in Python Inner Class
A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to
3 min read
Iterate over a set in Python
The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. You can use a for loop to access and process each element, but the sequence may change with each execution. Let's explore different ways to iterate over a set.Using for loopWh
2 min read
type and isinstance in Python
In this article, we will cover about type() and isinstance() function in Python, and what are the differences between type() and isinstance(). What is type in Python? Python has a built-in method called type which generally comes in handy while figuring out the type of the variable used in the progr
5 min read