Python getattr() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The getattr() function in Python facilitates accessing attribute values of objects and provides the flexibility to execute a default value if the specified key is unavailable.

Example

Output:

Syntax of Python getattr() Function

getattr() function in Python takes an object as a parameter and the name of the attribute as a parameter.

If the name of the attribute is not found in the Python object then the default value is thrown as output.

The syntax of the getattr Python function is the same as object.name..

You can read more about Python objects .

Parameters of Python getattr() Function

getattr Python function takes three parameters as follows :

  • object : name of the object
  • attribute name : attribute name of the object
  • default value (optional) : default value of the attribute.

Return Value of Python getattr() Function

The return value of the getattr() Python function is :

  • value of the named attribute passed to the getattr() function.
  • default value if the named attribute is not found.
  • AttributeError if the named attribute is not found and the default value is not passed to the function parameter.

Working on Python getattr() Function

Let's take an example to understand the working of the getattr Python function.

Example 1:

Output

In the above Example 1, we are creating a Fruit class with two named attributes i.e name and color. When getattr(Fruit, "name") is called it looks for the attribute with key "name" and if it is found it returns the value of the named attribute i.e Apple in the above Case.

In the above Example 1, we are also creating a fruit object of class Fruit. If access to the name attribute is by the fruit object it also returns the same value which was returned by getattr(Fruit,"name") i.e Apple.

Example 2:

working of the getattr() Python function when the name of the attribute is not found.

Output

In the above Example 2, we can see that when the named attribute is not present in the object but the default value is passed as a parameter then the default value is thrown as output.

If the default value is not provided to the getattr Python function and the named attribute is not found in the object then the function gives AttributeError as shown in the above Example 2 output.

Python getattr Function Examples

Let's look at the different ways in which we can use the getattr() function.

a) Working on getattr Python function

Output

b) getattr when the named attribute is not found

In the above example, the case where the description attribute is not found refers to the case where the getattr() function when the named attribute is not found.

When the default value is not found and if the default value is passed to the parameter then the default value is thrown as output and if the default value is not found then the program throws AttributeError.

c) getattr() Function Python Default Value

The default value of the named attribute is passed to the getattr() Python function. When the named attribute is not found in the Python object then the default value passed to the getattr() function is returned.

If Default value is not passed to the getattr() function and the named attribute is not found in the pyhton object then AttributeError is generated by getattr Python function.

Applications of getattr() function

The getattr() function in Python is commonly used in various applications and scenarios. Such as:

  • Accessing attributes dynamically
  • Handling optional attributes
  • Configuring objects
  • API integration

Conclusion

  • getattr() function is used in Python to get the named attribute of the object in Python.
  • getattr() Python function takes the object name, attribute name, and default value(optional) as parameters.
  • getattr() Python function returns the named attribute value from the Python object if it is present.
  • If the named attribute is not present then the default value of the named attribute passed as a parameter is returned as output by the getattr() function.
  • If the named attribute is not found and the default value is also not passed as a parameter to the getattr() function then the function gives AttributeError.

functions in Python.