Python access parent class attribute

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.

Example:

class Student:

    stream = 'COE'

    def __init__(self, name, roll_no):

        self.name = name 

        self.roll_no = roll_no 

a = Student('SHIVAM', 3425

b = Student('SACHIN', 3624)

print(a.stream)

print(b.stream)

print(a.name)

print(b.name)

print(a.roll_no) 

print(b.roll_no)

print(Student.stream)

Output:

COE
COE
SHIVAM
SACHIN
3425
3624
COE

Note: For more information, refer to Python Classes and Objects.

Accessing Parent Class Functions

When a class inherits from another class it inherits the attributes and methods of another class. A class that inherits from another class is known as child class and the class from which the child class inherits is known as Parent class. But have you ever wondered how to access the parent’s class methods? This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.

Example:

class Person( object ):     

        def __init__(self, name, idnumber):    

                self.name = name 

                self.idnumber = idnumber 

        def display(self): 

                print(self.name) 

                print(self.idnumber) 

class Employee( Person ):            

        def __init__(self, name, idnumber, salary): 

                self.salary = salary 

                Person.__init__(self, name, idnumber)  

        def show(self):

            print(self.salary)

a = Employee('Rahul', 886012, 30000000)     

a.display()

a.show() 

Output:

Rahul
886012
30000000

Note: For more information, refer to Inheritance in Python.

Accessing Parent class method from inner class

An inner class or nested class is a defined inside the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have one or more than one inner classes.

Types of Inner Classes:

  • Multiple Inner Class
  • Multilevel Inner Class

Multiple Inner Class: A class containing more than one inner class.

Python access parent class attribute

Example:

class Electronics:

    def __init__(self):

        print('SINGLA ELECTRONICS')

        self.laptop=self.Laptop()

        self.mobile=self.Mobile()

    class Laptop:

        def operation(self):

            print('DELL Inspiron 15')

    class Mobile:

        def operation(self):

            print('Redmi Note 5')

ele = Electronics()

ele.laptop.operation()

ele.mobile.operation()

Output:

SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5

Multilevel Inner Class: In multilevel inner classes, the inner class contains another class which is inner classes to the previous one.

Python access parent class attribute

Example:

class Vehicle:

    def __init__(self):

        self.inner = self.Car()

        self.innerinner = self.inner.Maruti()

    def show_classes(self):

        print("This is in Outer class that is Vehicle")

    class Car:

        def __init__(self):

            self.innerinner = self.Maruti()

        def show_classes(self):

            print("This is in Inner class that is Car")

        class Maruti:

            def inner_display(self, msg):

                print("This is in multilevel InnerInner\

                      class that is Maruti")

                print(msg)

outer = Vehicle()

outer.show_classes()

inner = outer.Car()

inner.show_classes()

innerinner = inner.Maruti()

innerinner.inner_display("Just Print It!")

Output:

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!

How do you use parent class in Python?

This can generally be achieved by two ways. Using Classname: Parent's class methods can be called by using the Parent classname. method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly.

How do you access the variable of a parent class?

Use of super with variables. This scenario occurs when a derived class and base class has the same data members. ... .
Use of super with methods. This is used when we want to call the parent class method. ... .
Use of super with constructors. The super keyword can also be used to access the parent class constructor..

What is __ MRO __ in Python?

Method Resolution Order(MRO) it denotes the way a programming language resolves a method or attribute. Python supports classes inheriting from other classes. The class being inherited is called the Parent or Superclass, while the class that inherits is called the Child or Subclass.

Can a parent class access child class variable?

The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.