Skip to content
Built-in Functions

getattr()

Read an attribute when its name is stored in a string.

getattr(object, name, default=None)

getattr() gets a named value from an object or class. A default can prevent an error when the attribute is missing.

Parameters

NameDescription
objectThe object or class to inspect.
nameThe attribute name as a string.
defaultOptional value to return if the attribute is missing.

Returns

The attribute value, or the supplied default.

Try it

Run it and change it
class User:
    name = "Ada"
print(getattr(User, "name"))

Related entries