Menu Close

Python getattr() Function

Python getattr() function

In this built-in function tutorial, we are going to learn all about the Python getattr() function to return the value of a specified attribute of the specified object. To understand this Python, you should have basic knowledge of the Python class and object.

Python getattr() function

Python getattr function is a Python built-in function that is used to get the value of a specified attribute from the specified object. Python getattr() function gives another facility to assign a default value if the specified attribute does not exist. It comes with Python by default because it is built-in function.

Syntax

The syntax of getattr function in Python is:-

getattr(object, attribute, value)

Parameter

getattr() function in Python supports three parameters.

  • object:- Required, An object.
  • attribute:- The name of the attribute you want to value from.
  • value:- Value to be returned, If the specified attribute does not exist.

Return Value

The return value of the Python getattr() function is the value of the passed attribute.

Python getattr() function examples

In this example section, we are about to see multiple examples of getattr() function. To understand Python getattr function, we have created a simple class named Student.

Example: Getting value of attribute using getattr()


# Create class 
class Student:
    name = 'Vishvajit'
    roll = 100
    course = 'BCA'
    occupation = 'Programmer'
    

#Use getattr function to get the value of name attribute.
result = getattr(Student, 'name')
print(result)

The output will be:- Vishvajit

What happens, If the specified attribute does not exist, let’s see.

Example: What happen if passed attribute does not exist


# Create class 
class Student:
    name = 'Vishvajit'
    roll = 100
    course = 'BCA'
    occupation = 'Programmer'
    

#Use getattr function to get the value of the address attribute that does not exist.
result = getattr(Student, 'address')
print(result)

If you will execute the above program, you will get an error like below.

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 10, in <module>
    result = getattr(Student, 'address')
AttributeError: type object 'Student' has no attribute 'address'

To solve the above problem, you can use the python getattr function’s third parameter which is the default value.

Example: Using getattr() function with a default value


# Create class 
class Student:
    name = 'Vishvajit'
    roll = 100
    course = 'BCA'
    occupation = 'Programmer'
    

#Use getattr function to get the value of address attribute.
result = getattr(Student, 'address', 'Noida')
print(result)

The output will be:- Noida

Conclusion

So In this article, you have learned the Python getattr() function to get the value of specified attributes from the specified object along with various examples. It also accepts a default value and this value will only return if the specified attributes do not belong to the specified object.

If this article really helpful for your, please share it with your friends who want to learn this type of python program.

Other Python built-in functions


For more information:- Click Here

Thanks for your valuable time… 🙏🙏❤️❤️

Python hasattr() Function
Python enumerate() Function

Related Posts