Python setattr() Function
Example
Change the value of the "age" attribute of the "person" object:
class Person: name = "John" age = 36 country = "Norway" setattr(Person, 'age', 40)
Definition and Usage
The setattr() function specifies the value of the specified attribute of the object.
Syntax
setattr(object, attribute, value)
Parameter Value
Parameter | Description |
---|---|
object | Required. Object. |
attribute | Required. The name of the attribute you want to set. |
value | Required. Needed. The value you want to assign to the specified attribute. |
Related Pages
Reference Manual:delattr() Function(Delete the attribute)
Reference Manual:getattr() Function(Get the value of the attribute)
Reference Manual:hasattr() Function(Check if the attribute exists)