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.
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Reading Python File-Like Objects from C | Python
- Python | Numbers in a list within a given range
- Python | Generate random numbers within a given range and store in a list
- Python | Find elements within range in numpy
- Python | Count unique sublists within list
- Python - Filter Strings within ASCII range
- Byte Objects vs String in Python
- File Objects in Python
- Timer Objects in Python
- Barrier Objects in Python
- Python | Counter Objects | elements()
- Mutable vs Immutable Objects in Python
- Python Tkinter | Moving objects using Canvas.move() method
- Python | Unit Test Objects Patching | Set-1
- Python | Unit Test Objects Patching | Set-2
- Python Classes and Objects
- Python objects
- Print objects of a class in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.