Home >>Python Built-in Functions >Python getattr() Function
Python getattr() function is used to return the value of the given input attribute from the required object and also give an option of executing the default value when the key is unavailable.
Syntax:getattr(object, attribute, default)
Parameter | Description |
---|---|
object | This is a required parameter. It defines the given object. |
attribute | It defines the name of the attribute you want to get the value from. |
default | This is an optional parameter. It defines the value to return if the attribute does not exist. |
class Person:
name = "Jerry"
age = 21
country = "India"
x = getattr(Person, 'country')
print(x)
class Person:
name = "Jerry"
age = 21
country = "India"
x = getattr(Person, 'Job', 'Developer')
print(x)