getattr

getattr(object, name [,default])

Returns the named attribute (built-in).

objectobject whose named attribute's value is to be returned
namestring that contains the attribute's name
default(optional) value that is returned when the named attribute is not found

REMARKS
* This is a built-in function.
* Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised. name need not be a Python identifier (see setattr()).
* Since private name mangling happens at compilation time, one must manually mangle a private attribute's (attributes with two leading underscores) name in order to retrieve it with getattr().
* You can use the delattr function to
* You can use the hasattr function to
* You can use the setattr function to
* For the Official documentation refer to python.org

class Person: 
age = 23
name = "Adam"

person = Person()
print('The age is:', getattr(person, "age"))
print('The age is:', person.age)

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top