How to Access a Parent Class Attribute in Python?


In object-oriented programming, inheritance allows us to create new classes that inherit the properties and methods of an existing class. This powerful concept enables code reuse, modularity, and extensibility in our programs. Before diving into accessing parent class attributes, let's have a quick refresher on inheritance. In Python, when a class inherits from another class, it acquires all the attributes and methods defined in the parent class. This mechanism allows us to create specialized classes that inherit and extend the functionality of a more general base class. The derived class is also known as a child class, while the class being inherited from is called the parent class or base class.

Example

Here's a simple example to illustrate the concept of inheritance −

class Parent:
   def __init__(self):
      self.parent_attribute = "I'm from the parent class"

class Child(Parent):
   def __init__(self):
      super().__init__()
      self.child_attribute = "I'm from the child class"

child = Child()
print(child.parent_attribute)  # Accessing parent class attribute
print(child.child_attribute)

Output

I'm from the parent class
I'm from the child class

In this example, we have two classes: Parent and Child. The Child class inherits from the Parent class using the syntax class Child(Parent). This means that the Child class inherits all the attributes and methods defined in the Parent class. The Child class also has its own attribute called child_attribute.

Accessing Parent Class Attribute

To access a parent class attribute in Python, you can use the dot notation along with the instance or class name. The approach you choose depends on the context and your specific requirements. Let's explore the different methods to access parent class attributes:

Using the Instance

If you have an instance of the child class, you can access the parent class attribute directly through the instance. The instance retains all the attributes and methods inherited from the parent class, allowing you to access them effortlessly.

Example

Here's an example 

class Parent:
   def __init__(self):
      self.parent_attribute = "I'm from the parent class"

class Child(Parent):
   def __init__(self):
      super().__init__()
      self.child_attribute = "I'm from the child class"

child = Child()
print(child.parent_attribute)  # Accessing parent class attribute using instance

Output

I'm from the parent class

In this example, child.parent_attribute accesses the parent_attribute defined in the parent class. By accessing the attribute through the instance, you can retrieve the value associated with that attribute.

Using the Class Name

In addition to accessing parent class attributes through an instance, you can also access them using the child class name. This approach is useful when you don't have an instance available, but you still want to access the parent class attribute directly.

Example

Here's an example 

class Parent:
   parent_attribute = "I'm from the parent class"

class Child(Parent):
   child_attribute = "I'm from the child class"

print(Child.parent_attribute)  # Accessing parent class attribute using class name

Output

I'm from the parent class

In this case, Child.parent_attribute accesses the parent_attribute defined in the parent class. By using the class name, you can directly access the parent class attribute without the need for an instance.

Accessing Parent Class Methods

Inheritance not only allows us to access parent class attributes but also grants us access to parent class methods. When a child class inherits from a parent class, it inherits all the methods defined in the parent class. This means that you can invoke those methods in the child class using instances or the class name.

Example

Here's an example 

class Parent:
   def parent_method(self):
      print("This is a method from the parent class")

class Child(Parent):
   def __init__(self):
      super().__init__()

child = Child()
child.parent_method()  # Accessing parent class method using instance
Child.parent_method()  # Accessing parent class method using class name

Output

This is a method from the parent class
This is a method from the parent class

In this example, the Child class inherits the parent_method from the Parent class. We can invoke this method using an instance of the Child class (child.parent_method()) or directly using the class name (Child.parent_method()).

Overriding Parent Class Attributes

In some cases, you may need to override a parent class attribute in the child class. Overriding means providing a different value or behavior for a specific attribute in the child class. By redefining the attribute in the child class, you can customize its value while still having access to the parent class attribute using the techniques discussed earlier.

Example

Here's an example 

class Parent:
   def __init__(self):
      self.shared_attribute = "I'm from the parent class"

class Child(Parent):
   def __init__(self):
      super().__init__()
      self.shared_attribute = "I'm from the child class"

child = Child()
print(child.shared_attribute)  # Accessing overridden attribute

Output

I'm from the child class

In this example, both the Parent and Child classes have an attribute called shared_attribute. However, in the child class, we redefine the attribute with a different value. When we access the attribute using an instance of the child class (child.shared_attribute), we retrieve the overridden value defined in the child class.

Multiple Inheritance

Python supports multiple inheritance, which means a class can inherit from multiple parent classes. When working with multiple inheritance, accessing parent class attributes can become more complex. In such cases, you may need to explicitly specify which parent class attribute you want to access using the method resolution order (MRO) or the super() function.

Example

Here's an example of multiple inheritance and accessing parent class attributes 

class Parent1:
   def __init__(self):
      self.shared_attribute = "I'm from Parent1"

class Parent2:
   def __init__(self):
      self.shared_attribute = "I'm from Parent2"

class Child(Parent1, Parent2):
   def __init__(self):
      super().__init__()

child = Child()
print(child.shared_attribute)  # Accessing parent class attribute in multiple inheritance

Output

I'm from Parent1

In this example, the Child class inherits from both Parent1 and Parent2 classes. When we create an instance of the Child class and access the shared_attribute, it retrieves the value defined in Parent1.

Protected and Private Attributes

Protected attributes are typically prefixed with a single underscore (_), indicating that they should not be accessed directly outside the class but can still be accessed by child classes. Private attributes, on the other hand, are typically prefixed with double underscores (__), indicating that they are intended to be accessed only within the class itself.

Example

Here's an example 

class Parent:
   def __init__(self):
      self._protected_attribute = "I'm a protected attribute"
      self.__private_attribute = "I'm a private attribute"

class Child(Parent):
   def __init__(self):
      super().__init__()

child = Child()
print(child._protected_attribute)   # Accessing protected attribute
print(child._Parent__private_attribute)   # Accessing private attribute

Output

I'm a protected attribute
I'm a private attribute

In this example, the Parent class has a protected attribute _protected_attribute and a private attribute __private_attribute. The child class, Child, can still access both attributes. However, accessing the private attribute requires using a name mangling technique with the format _ClassName__private_attribute.

Conclusion

Inheritance is a powerful feature that allows us to create class hierarchies and build upon existing functionality. By accessing parent class attributes, we can leverage code reuse and modularity in our programs.

We learned that accessing parent class attributes can be done using either the instance or class name. Through practical examples, we saw how to access parent class attributes using an instance of the child class and how to directly access them using the class name.

Updated on: 16-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements