Python is a dynamic language that allows programmers to manipulate objects at runtime. One of the ways to access object attributes dynamically is through the getattr()
method. In this article, we will explore the getattr()
method in detail and provide examples of how it can be used.
What is Python getattr()
Method?
The getattr()
method is a built-in function in Python that returns the value of a named attribute of an object. It takes two or three arguments: the object, the name of the attribute, and an optional default value. If the attribute does not exist and no default value is provided, it raises an AttributeError.
The syntax for getattr()
is as follows:
getattr(object, name[, default])
Here, object
is the object whose attribute we want to access, name
is the name of the attribute we want to access, and default
is an optional argument that specifies the value to be returned if the attribute is not found.
How to Use Python getattr()
Method
Let’s explore some examples of how to use getattr()
method in Python.
Example 1: Get Attribute of an Object
class MyClass:
x = 10
y = 20
obj = MyClass()
print(getattr(obj, 'x')) # Output: 10
print(getattr(obj, 'y')) # Output: 20
In this example, we define a class MyClass
with two attributes x
and y
. We create an object obj
of the class and use getattr()
method to get the values of the attributes.
Example 2: Get Attribute of a Module
import math
print(getattr(math, 'pi')) # Output: 3.141592653589793
In this example, we import the math
module and use getattr()
method to get the value of the pi
attribute.
Example 3: Get Attribute with Default Value
class MyClass:
x = 10
y = 20
obj = MyClass()
print(getattr(obj, 'z', 'Attribute not found')) # Output: Attribute not found
In this example, we try to access an attribute z
that does not exist in the object obj
. We pass a default value 'Attribute not found'
as the third argument to getattr()
. Since z
attribute is not found, the method returns the default value.
Example 4: Get Method of an Object
class MyClass:
def add(self, x, y):
return x + y
obj = MyClass()
method = getattr(obj, 'add')
print(method(2, 3)) # Output: 5
In this example, we define a class MyClass
with a method add
. We create an object obj
of the class and use getattr()
method to get the method. We then call the method with arguments 2 and 3.
Example 5: Get Attribute of a Class
class MyClass:
x = 10
y = 20
print(getattr(MyClass, 'x')) # Output: 10
In this example, we use getattr()
method to get the value of the attribute x
of the class MyClass
.
Related Concepts and Methods
hasattr()
The hasattr()
method is another built-in function in Python that returns True
if the object has the named attribute, and False
otherwise. It takes two arguments: the object and the name of the attribute.
The syntax for hasattr()
is as follows:
hasattr(object, name)
Here, object
is the object we want to check for the attribute, and name
is the name of the attribute we want to check.
setattr()
The setattr()
method is a built-in function in Python that sets the value of a named attribute of an object. It takes three arguments: the object, the name of the attribute, and the value to be set.
The syntax for setattr()
is as follows:
setattr(object, name, value)
Here, object
is the object whose attribute we want to set, name
is the name of the attribute we want to set, and value
is the value to be set.
Conclusion
In this article, we explored the getattr()
method in Python and provided examples of how it can be used to access object attributes dynamically. We also discussed related concepts and methods such as hasattr()
and setattr()
. By using these methods, you can make your Python code more dynamic and flexible.