The getattr()
function returns the value of the specified attribute from the specified object.
Lets create a class Person
first which have attributes name
, age
and country
.
When we call getattr()
it will take 3 arguments as (object, attribute, default)
. Here we gave the arguments as Object=Person
, attribute='age'
. Default is used to print any statement when the attribute is not found.
class Person:
name = "John"
age = 36
country = "Norway"
x = getattr(Person, 'age')
Here the answer given by this is 36.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…