Prerequisite: Basics of OOPs in Python
In this article, we will learn how to access object methods and attributes within other objects in Python. If we have two different classes and one of these defined another class on calling the constructor. Then, the method and attributes of another class can be accessed by first class objects ( i.e; objects within objects ).
Here in the below example we learn to access object (its methods and attributes) within an object. We define two classes (class first and class second) with proper definitions.
- First class consists of a constructor and a method.
- The constructor form an object of the second class in the attribute of the first class.
- The method defines the presence in first class method.
- Similarly, the second class consists of a constructor and a method.
- The constructor form an attribute.
- The method defines the presence in the second class method.
As the attribute of first class work as an object of the second class so all the methods and attributes of the second class can be accessed using this:
Below is the implementation:
<__main__.first object at 0x7fde6c57b828> <__main__.second object at 0x7fde6c57b898> GFG Inside second method
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course