Python getattr() function
Python getattr() function
The getattr() function in Python returns the value of the named attribute for the given object.
Syntax
getattr(object, name[, default])
Parameter
object: It is a required parameter which represents an object.
name: This parameter represents the name of the attribute you want to get the value from.
default: It represents the value to return if the attribute does not exist.
Return
This function returns the value of the given attribute from the specified object.
Example 1
# Python program explaining
# the getattr() function
# declaring a class
class EAT :
name = "ExamplesAndTutorials"
acive_years = 14
# initializing the object
obj = EAT()
# using getattr() function
print("The name is " + getattr(obj,'name'))
Output
The name is ExamplesAndTutorials
