Definition
The setattr() function sets the value of the specified attribute of the specified object.
Syntax
setattr(object, attribute, value)
Parameters
Parameter | Description |
---|---|
object | Required. An object. |
attribute | Required. The name of the attribute you want to set |
value | Required. The value you want to give the specified attribute |
Examples
class Car:
name = "Ford"
model = 1987
country = "USA"
print("Old value: " + str(Car.model))
setattr(Car, 'model', 1996)
print("New value: " + str(Car.model))