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)

Run Example

Definition and Usage

setattr() Function sets 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. You want to assign a value 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)