setattr()Easy Examples

Sets the value of a named attribute on an object

Basic setattr() usage

Simple demonstration of the setattr() built-in function.

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.

setattr() with different inputs

Calling setattr() with various argument types.

python
# More setattr() examples
print("setattr() is a Python built-in function")
print(f"Type: {type(setattr)}")

setattr() accepts different types of arguments and produces corresponding results.

Want to try these examples interactively?

Open Easy Playground