Python - Access Parent Class Attribute
Last Updated :
02 Jul, 2020
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:
Python3
# Python program to demonstrate
# classes and objects
# Creating a class
class Student:
# Class Variable
stream = 'COE'
def __init__(self, name, roll_no):
# Instance Variable
self.name = name
self.roll_no = roll_no
# Objects of Student class
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)
# Class variables can be
# accessed using class
# name also
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:
Python3
# Python code to demonstrate
# how parent constructors are called.
# parent class
class Person( object ):
# __init__ is known as the constructor
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)
# child class
class Employee( Person ):
def __init__(self, name, idnumber, salary):
self.salary = salary
# invoking the constructor of
# the parent class
Person.__init__(self, name, idnumber)
def show(self):
print(self.salary)
# creation of an object
# variable or an instance
a = Employee('Rahul', 886012, 30000000)
# calling a function of the
# class Person using Employee's
# class instance
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.
Example:
Python3
class Electronics:
def __init__(self):
print('SINGLA ELECTRONICS')
self.laptop=self.Laptop()
self.mobile=self.Mobile()
# Inner Class 1
class Laptop:
def operation(self):
print('DELL Inspiron 15')
# Inner Class 2
class Mobile:
def operation(self):
print('Redmi Note 5')
# Driver Code
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.
Example:
Python3
class Vehicle:
def __init__(self):
# instantiating the 'Inner' class
self.inner = self.Car()
# instantiating the multilevel
# 'InnerInner' class
self.innerinner = self.inner.Maruti()
def show_classes(self):
print("This is in Outer class that is Vehicle")
# inner class
class Car:
# First Inner Class
def __init__(self):
# instantiating the
# 'InnerInner' class
self.innerinner = self.Maruti()
def show_classes(self):
print("This is in Inner class that is Car")
# multilevel inner class
class Maruti:
def inner_display(self, msg):
print("This is in multilevel InnerInner\
class that is Maruti")
print(msg)
# Driver Code
outer = Vehicle()
outer.show_classes()
inner = outer.Car()
inner.show_classes()
innerinner = inner.Maruti()
# Calling the method inner_display
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!
Similar Reads
How to Access dict Attribute in Python In Python, A dictionary is a type of data structure that may be used to hold collections of key-value pairs. A dictionary's keys are connected with specific values, and you can access these values by using the keys. When working with dictionaries, accessing dictionary attributes is a basic function
6 min read
Python Attributes: Class Vs. Instance Explained In Python, attributes are properties associated with objects. They can be variables or methods that are defined within a class or an instance of a class. Understanding the difference between class and instance attributes is fundamental in object-oriented programming. Here's an explanation: Python At
4 min read
Accessing Attributes and Methods in Python In Python, attributes and methods define an object's behavior and encapsulate data within a class. Attributes represent the properties or characteristics of an object, while methods define the actions or behaviors that an object can perform. Understanding how to access and manipulate both attributes
3 min read
How to Get a List of Class Attributes in Python? Getting a list of class attributes in Python means identifying all variables defined at the class level, excluding instance attributes and methods. For example, a class might have attributes like name, age and location. The output will be a list or dictionary showing these attribute names and their
3 min read
Abstract Base Class (abc) in Python Have you ever thought about checking whether the objects you are using adheres to a particular specification? It is necessary to verify whether an object implements a given method or property, especially while creating a library where other developers make use of it. A developer can use hasattr or i
7 min read
Call Parent class method - Python In object-oriented programming in Python, the child class will inherit all the properties and methods of the parent class when the child class inherits the parent class. But there may be some cases when the child class has to call the methods of the parent class directly. Python has an easy and effe
5 min read