SimpleTuts.com

Python Inheritance

Inheritance in Python refers to the capability of a class (called a subclass or derived class) to inherit properties and behaviors from another class (called a base class or superclass). This allows subclasses to reuse and extend the functionality of the superclass, promoting code reusability and reducing redundancy.


class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

class Student(Person):                  #Creates a class called Student that inherits the Person class
  def __init__(self, fname, lname, year):
    #The first name and last name of the student are set using the init function of the super class
    super().__init__(fname, lname)      
    self.graduationyear = year

  def welcome(self):
    print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

x = Student("John", "Doe", 2019)
x.welcome()
                
Python Online Compiler