The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.
Example
class Student: marks = 88 name = 'Sheeran' person = Student()name = getattr(person, 'name')
print(name)marks = getattr(person, 'marks')
print(marks) # Output: Sheeran # 88getattr() Syntax
The syntax of getattr() method is:
getattr(object, name[, default])The above syntax is equivalent to:
object.namegetattr() Parameters
getattr() method takes multiple parameters:
- object - object whose named attribute's value is to be returned
- name - string that contains the attribute's name
- default (Optional) - value that is returned when the named attribute is not found
getattr() Return Value
getattr() method returns:
- value of the named attribute of the given object
- getattr(object, name[, default])1, if no named attribute is found
- getattr(object, name[, default])2 exception, if named attribute is not found and getattr(object, name[, default])1 is not defined
Example 1: How getattr() works in Python?
class Person: age = 23 name = "Adam" person = Person()print('The age is:', getattr(person, "age"))
print('The age is:', person.age)Output
The age is: 23 The age is: 23Example 2: getattr() when named attribute is not found
class Person: age = 23 name = "Adam" person = Person() # when default value is providedprint('The sex is:', getattr(person, 'sex', 'Male'))
print('The sex is:', getattr(person, 'sex'))
Output
The sex is: Male AttributeError: 'Person' object has no attribute 'sex'The named attribute sex is not present in the class Person. So, when calling getattr() method with a default value getattr(object, name[, default])5, it returns Male.
But, if we don't provide any default value, when the named attribute sex is not found, it raises an getattr(object, name[, default])2 saying the object has no sex attribute.
Before we jump into how getattr can be a useful tool for DRYing up our code, lets cover how it works (docs )! In short, it takes an object and a string version of a property name, and returns the value of that property, if it exists on an object. It can optionally take a default value to return if the attribute can’t be found on the object, and otherwise…
The class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 1 method returns the value of the attribute of an object. If the named attribute does not exist, default is returned if provided, otherwise class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 2 is raised.
Syntax:
getattr(object, name, default)Parameters:
- object: An object of the class whose attribute value needs to be returned.
- name: The string name of the attribute.
- default. (Optional) A value to be returned if the attribute is not found.
Return Value:
- Returns value of the attribute of the given object.
- Returns the specified default value if attribute not found.
- If the default value not specified, then throws class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 2.
The following example demonstrates the class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 1 method.
Example: getattr()
Copyclass student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name'))
Output
student name is John student name changed to Bill
Above, class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 5 returns the value of the class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 6 property of the class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 7 object, which is class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 8. It always returns the latest value even after updating a property value.
If the attribute specified in the argument is not found, an class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 2 exception is thrown.
Example: getattr()
Copyclass student: name = 'John' age = '18' std = student() attr = getattr(std, 'subject')
Output
Traceback (most recent call last): attr = getattr(std, 'subject') AttributeError: type object 'student' has no attribute 'subject'
The default parameter can be passed to avoid the above error, which returns the default value if the attribute is not found.
Example: getattr()
Copyclass student: name = 'John' age = '18' std = student() subject = getattr(std, 'subject', 'Not supported') print("student's subject: ", subject)
Output
student's subject: Not supported
Instead of class student: name = 'John' age = 18 std = student() # creating object print('student name is ', getattr(std, 'name')) std.name = 'Bill' # updating value print('student name changed to ', getattr(std, 'name')) 1 method, the student name is John student name changed to Bill 1 operator can also be used to access the attribute value if you are sure that an object has that attribute.