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>


  • 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.


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


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): = name self.lastname = lastname def printname (self): print (, 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): = name self.lastname = lastname def printname (self): print (, 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?

Python inheritance log: Questions


Python"s equivalent of && (logical-and) in an if-statement

5 answers

delete By delete

Here"s my code:

def front_back(a, b):
  # +++your code here+++
  if len(a) % 2 == 0 && len(b) % 2 == 0:
    return a[:(len(a)/2)] + b[:(len(b)/2)] + a[(len(a)/2):] + b[(len(b)/2):] 
    #todo! Not yet done. :P

I"m getting an error in the IF conditional.
What am I doing wrong?


Answer #1

You would want and instead of &&.


Answer #2

Python uses and and or conditionals.


if foo == "abc" and bar == "bac" or zoo == "123":
  # do something

Python inheritance log: Questions


How do you get the logical xor of two variables in Python?

5 answers

Zach Hirsch By Zach Hirsch

How do you get the logical xor of two variables in Python?

For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or the empty string):

str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
    print "ok"
    print "bad"

The ^ operator seems to be bitwise, and not defined on all objects:

>>> 1 ^ 1
>>> 2 ^ 1
>>> "abc" ^ ""
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: "str" and "str"

Answer #1

If you"re already normalizing the inputs to booleans, then != is xor.

bool(a) != bool(b)


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method