Skip to content
Built-in Functions

setattr()

Set an attribute on an object using its name as text.

setattr(object, name, value)

setattr() is like writing object.name = value, but the attribute name is a string.

It is useful when an attribute name is stored in a variable.

Parameters

NameDescription
objectThe object to change.
nameThe attribute name as a string.
valueThe value to assign.

Returns

None.

Try it

Run it and change it
class Box: pass
box = Box()
setattr(box, "colour", "blue")
print(box.colour)

Related entries