On this page
setattr
Prev: set | Up: __builtins__ | Next: slice
Rel: callable, delattr, dir, getattr, hasattr
Ref: docs.python.org — setattr
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import setattr
Overview & Signature
setattr(obj, name, value, /)
Sets the named attribute on the given object to the specified value.
Usage
class Cfg: pass
c = Cfg()
setattr(c, "host", "localhost")
assert c.host == "localhost"
Gotchas
- Triggers
@propertysetters and__setattr__: Dynamic equivalent ofobj.name = value.
REPL Inspect
help(setattr)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/set Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/slice Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/callable Linked here; shared tags: #builtins
- tuts/python3/__builtins__/delattr Linked here; shared tags: #builtins
- tuts/python3/__builtins__/dir Linked here; shared tags: #builtins
- tuts/python3/__builtins__/getattr Linked here; shared tags: #builtins
- tuts/python3/__builtins__/hasattr Linked here; shared tags: #builtins
- tuts/python3/python metaprogramming links to this page