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
| Name | Description |
|---|---|
| object | The object or class to inspect. |
| name | The attribute name as a string. |
| default | Optional 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"))