Table of Contents
This guide covers inheritance (inheritance) in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
What Is Inheritance?
Inheritance Allows a class (class) to inherit properties and methods from other defined classes. The class has been called a parent class (base class or parent class), the newly derived class is called a subclass (child class or derived class). The subclass inherits all elements of the superclass, which can extend inheritance elements and add new components.
Syntax of inheritance
class BaseClass:Body of base classclass DerivedClass(BaseClass):Body of derived class
For more clarity about using inheritance, let's take an example.
Polygon is a closed shape with 3 or more sides. We have a class called DaGiac Defined as follows.
class DaGiac:def __init__(self, socanh):self.n = socanhself.canh = [0 for i in range(socanh)]def nhapcanh(self):self.canh = [float(input("Bạn hãy nhập giá trị cạnh "+str(i+1)+" : ")) for i in range(self.n)]def hienthicanh(self):for i in range(self.n):print("Giá trị cạnh",i+1,"là",self.canh[i])
Class DaGiac Has the n attribute to define the number of edges and edges to store each edge value. The Nhapcanh () Function takes the size of the edges and Hienthicanh () Displays the list of polygons.
The triangle is polygon with three sides, so we will create a class TamGiac Inherited from DaGiac. This new class will inherit all the properties available in the superclass, so you won't need to re-declare (reuse code). This TamGiac Is declared as follows:
class TamGiac(DaGiac):def __init__(self):DaGiac.__init__(self,3)def dientich(self):a, b, c = self.canh# Tính nửa chu vis = (a + b + c) / 2area = (s*(sa)*(sb)*(sc)) ** 0.5print('Diện tích của hình tam giác là %0.2f' %area)
The TamGiac Class not only inherits but also defines a new function as the Dientich Function .
>>> t = TamGiac()>>> t.nhapcanh()Bạn hãy nhập giá trị cạnh 1 : 3Bạn hãy nhập giá trị cạnh 2 : 5Bạn hãy nhập giá trị cạnh 3 : 4>>> t.hienthicanh()Giá trị cạnh 1 là 3.0Giá trị cạnh 2 là 5.0Giá trị cạnh 3 là 4.0>>> t.dientich()Diện tích của hình tam giác là 6.00
You can see that the functions Nhapcanh (), hienthicanh () Are not in TamGiac class, but we still use them
Overriding in Python
Python allows to override parent class methods. You can perform the override of the parent class method if you want to have special or special features in the subclass.
In the above example, we see that the TamGiac Class uses the __init __ () Function from the DaGiac Class , If you want to override the definition of __init __ () Function in the parent class, we use the super () function.
Super (). __ Init __ (3) Is equivalent to DaGiac. __ init __ (self, 3)
Also Python has two Isinstance () And Issubclass () functions that Are used to test the relationship of two classes and instances.
The Issubclass Function (classA, classB) Returns True if class A is a subclass of class B.
>>> issubclass(DaGiac,TamGiac)False>>> issubclass(TamGiac,DaGiac)True>>> issubclass(bool,int)True
The Isinstance (obj, Class) function Returns True if obj is an instance of Class or is an instance of Class's subclass.
>>> isinstance(t,TamGiac)True>>> isinstance(t,DaGiac)True>>> isinstance(t,int)False>>> isinstance(t,object)True
Previous lesson: Class and Object in Python
Previous article: Multiple Inheritance in Python
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.