setattr()
Built-in FunctionPython 2.0+Intermediate
Sets the value of a named attribute on an object
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
class Config: pass setattr(Config, "debug", True) setattr(Config, "version", "2.0") print(Config.debug, Config.version)
setattr() is a built-in function that sets the value of a named attribute on an object.
Try in PlaygroundTags
builtinfunctioncoreoopintrospectiondynamic
Related Items
getattr()
Built-in Function
Returns the value of a named attribute of 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
__setattr__
Dunder Method
Called on every attribute assignment
vars()
Built-in Function
Returns the __dict__ attribute of an object