
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I find the methods or attributes of an object in Python?
To find the attributes of an object, use the getarr() method in Python. To check if an attribute exist or not, use the hasattr() method. Set an attribute using the setattr() method in Python.
Access the attributes of an object
Example
To access the attributes of an object, we will use the getattr() method in Python −
class student: st_name ='Amit' st_age ='18' st_marks = '99' def demo(self): print(self.st_name) print(self.st_age) print(self.st_marks) # Create objects st1 = student() st2 = student() # The getattr() is used here print ("Name = ",getattr(st1,'st_name')) print ("Age = ",getattr(st2,'st_age'))
Output
Name = Amit Age = 18
Access and set class attributes
Example
In this example, to set the attributes, we will use the setattr() method.
class student: st_name ='Tim' st_age ='18' def demo(self): print("Hello from demo() function") # The getattr() is used here print(getattr(student,'st_name')) # Returns true if object has attribute print(hasattr(student,'st_age')) # Set additional attribute st_marks setattr(student,'st_marks','95') # Get Attribute print(getattr(student,'st_marks')) # Checking for an attribute print(hasattr(student,'demo'))
Output
Tim True 95 True
Access methods
Example
In this example, we will learn how to access methods −
class student: st_name ='Tim' st_age ='18' def demo(self): print("Hello from demo() function") # The getattr() is used here print(getattr(student,'st_name')) # Returns true if object has attribute print(hasattr(student,'st_age')) # Set additional attribute st_marks setattr(student,'st_marks','95') # Get Attribute print(getattr(student,'st_marks')) # Checking for an attribute print(hasattr(student,'demo')) # Access methods using an object st1 = student() st1.demo()
Output
Tim True 95 True Hello from demo() function
- Related Articles
- Accessing Attributes and Methods in Python
- What are the attributes of a file object in Python?
- how can I declare an Object Array in Java?
- How can I delete an item from an Object in MongoDB?
- How can my code discover the name of an object in Python?
- How can we find a good subset of the original attributes?
- How can I convert the arguments object to an array in JavaScript?
- In PHP, how can I add an object element to an array?
- How I can create Python class from JSON object?
- Accessing Attributes and Methods in C#
- How to define methods for an Object in JavaScript?
- What are the methods of an array object in JavaScript?
- How can I represent an infinite number in Python?
- What are the methods in jQuery to manipulate attributes?
- How can I find the number of arguments of a Python function?

Advertisements