getattr()
Built-in FunctionPython 2.0+Intermediate
Returns the value of a named attribute of an object
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
class Config: debug = True version = "1.0" print(getattr(Config, "debug")) print(getattr(Config, "missing", "default"))
getattr() is a built-in function that returns the value of a named attribute of an object.
Try in PlaygroundTags
builtinfunctioncoreoopintrospectiondynamic
Related Items
setattr()
Built-in Function
Sets the value of a named attribute on an object
hasattr()
Built-in Function
Returns True if an object has the given named attribute
delattr()
Built-in Function
Deletes a named attribute from an object
__getattr__
Dunder Method
Called when the default attribute access fails
vars()
Built-in Function
Returns the __dict__ attribute of an object