getattr(), setattr(), hasattr() and delattr()
Read, write, test and delete attributes by name at runtime.
Easy
getattr(device, "voltage", 0.0) # default if missing
setattr(config, key, value)
hasattr(obj, "read")
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.