Python getattr() Function

Python getattr() function is used to get the value of an object’s attribute. If the given attribute does not exist on the object, this function returns a default value.

class Student:
    name = 'John'
    age  = 24
 
student = Student()
 
print('Student name: ', getattr(student, 'name'))
print('Student age: ', getattr(student, 'age'))
 
# Output
# Student name:  John
# Student age:  24

getattr() Syntax

The syntax of the getattr() function is as follows:

getattr(object, attribute, defaultValue)

getattr() Parameters

The getattr() function accepts below three parameters:

  • object – The object whose attribute’s value is to be fetched
  • attribute – The name of the attribute whose value has to be fetched. It has to be a string.
  • defaultValue – A value that is to be returned if the given attribute does not exist.

getattr() Return Value

The getattr() function returns:

  • Actual value of the attribute for the given object if it exists
  • A default value if the given attribute does not exist on the object
  • AttributeError, if the givent attribute does not exist and no default value is supplied

getattr() Examples

In the example below, we have created a class Student which has an object named student. Let’s try to fetch the values of student’s name and age attributes using getattr() function.

Example 1: When the named attribute exists

class Student:
    name = 'John'
    age  = 24
 
student = Student()
 
print('Student name: ', getattr(student, 'name'))
print('Student age: ', getattr(student, 'age'))

Output:

Student name:  John
Student age:  24

Example 2: When the named attribute does not exist

In the above example, we have fetched the values of the name and age attributes and it worked as expected. Now, let’s try to fetch the value of an attribute that does not exist on the student object.

class Student:
    name = 'John'
    age  = 24
 
student = Student()
 
print('Student gender: ', getattr(student, 'gender'))

Output:

Traceback (most recent call last):
  File "<string>", line 7, in <module>
AttributeError: 'Student' object has no attribute 'gender'

It didn’t work right. Now the question is, can we anyhow avoid getting this error? Well, the answer to this is in the next example.


Example 3: Passing the default parameter to the gettattr() function

In the last example, we saw that when we tried to access the value of an attribute that does not exist on the object, we got an error. Actually, this is the main advantage of the getattr() function over the traditional dot(.) operator that we can pass a default value to the getattr() function.

If the attribute does not exist, the getattr() function returns the default value and if it does, it will return its original value. See the example below:

class Student:
    name = 'John'
    age  = 24
 
student = Student()

# Pass a default value Male 
print('Student gender: ', getattr(student, 'gender', 'Male'))

Output:

Student gender:  Male

As you can see from the above output, the attribute named ‘gender’ does not exist on the student object still it returned a value ‘Male’. This is because we passed the third parameter to the getattr() function which is returned as a default value in case the given attribute does not exist.


Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts