Python inheritance

| | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

In the world of object-oriented programming (OOP), inheritance refers to the mechanism of a class’s ability to inherit or extend properties of another class at runtime. This property allows a derived class to obtain properties or traits of a base class.

Inheritance in Python is considered one of the most important aspects of OOP because it serves as a reusable function, which makes a piece of code more robust.

inheritance flowchart



< / p>

Benefits

  • Inheritance describes relationships that resemble real-world scenarios.
  • It provides a reusability that allows the user to add additional functionality to a derived class without changing it.
  • If the class Y inherits from class X, then automatically all subclasses of Y will inherit from class X.

Basic inheritance terms

  1. Subclass / derived class: this is a class that inherits properties from another class (usually a base class).
  2. Superclass / base class: This is the class from which other subclasses come from.
  3. A derived class usually derives / inherits / extends a base class.

Syntax

 class SuperClassName: Body of Super class class DerivedClass_Name (SuperClass): Body of de rived class 

Examples

Let’s dive into the world of Python inheritance with simple examples. Read also about multiple inheritance .


< br>

Step 1. Create a base class

 class Father: # The keyword ’self’ is used to represent the instance of a class. # By using the "self" keyword we access the attributes and methods of the class in python. # The method "__init__" is called as a constructor in object oriented terminology. # This method is called when an object is created from a class. # it allows the class to initialize the attributes of the class. def __init __ (self, name, lastname): self.name = name self.lastname = lastname def printname (self): print (self.name, self.lastname) # Use the Father class to create an object, and then execute the printname method: x = Father ("Anees", "Mulani") x.printname () 

Output: Anees Mulani

Step 2. Create a derived class

 # The subclass __init __ () function overrides the inheritance of the base class __init __ () function. class Son (Father): def __init __ (self, name, lastname): Father .__ init __ (self, name, lastname) x = Son ("Dev", "Bajaj") x.printname () 

Conclusion: Dev Bajaj

Using the super () function

Using the super () function, you don’t need to use the parent element name, it will automatically inherit the methods and properties from its parent.

 class Father: def __init __ (self, name, lastname): self.name = name self.lastname = lastname def printname (self): print (self.name, self.lastname) class Son (Father): def __init __ (self, name, lastname): super () .__ init __ (name, lastname) x = Student ("Dev", "Bajaj") x.printname () 



👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python inheritance, check other ast Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Marie Zelotti

San Francisco | 2023-01-27

Maybe there are another answers? What Python inheritance exactly means?. Will use it in my bachelor thesis

Olivia Nickolson

Warsaw | 2023-01-27

Ev PHP module is always a bit confusing 😭 Python inheritance is not the only problem I encountered. Will use it in my bachelor thesis

Walter Innsbruck

California | 2023-01-27

I was preparing for my coding interview, thanks for clarifying this - Python inheritance in Python is not the simplest one. Checked yesterday, it works!

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically