
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Class Inheritance in Python
Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name.
The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent.
Syntax
Derived classes are declared much like their parent class; however, a list of base classes to inherit from is given after the class name −
class SubClassName (ParentClass1[, ParentClass2, ...]): 'Optional class documentation string' class_suite
Example
#!/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 Child(Parent): # 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
Output
When the above code is executed, it produces the following result −
Calling child constructor Calling child method Calling parent method Parent attribute : 200
Similar way, you can drive a class from multiple parent classes as follows −
class A: # define your class A ..... class B: # define your class B ..... class C(A, B): # subclass of A and B .....
You can use issubclass() or isinstance() functions to check a relationships of two classes and instances.
- The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of the superclass sup.
- The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instance of a subclass of Class
- Related Articles
- How does class inheritance work in Python?
- How does class variables function in multi-inheritance Python classes?
- Inheritance in Python
- How to prevent class inheritance in C++
- Golang Program to Show Inheritance in Class
- C++ program to test inheritance through triangle class
- Introduction to Classes and Inheritance in Python
- Does Python support multiple inheritance?
- How we can extend multiple Python classes in inheritance?
- How many types of inheritance are there in Python?
- Explain Inheritance vs Instantiation for Python classes.
- Can you explain what is metaclass and inheritance in Python?
- Inheritance in Java
- Program to find out the inheritance order in a family in Python
- self in Python class
