Home >>Python Built-in Functions >Python setattr() Function
Python setattr() function is used to set the value of the specified attribute of the given input object.
Syntax:setattr(object, attribute, value)
Parameter | Description |
---|---|
object | This is a required parameter. It defines an object. |
attribute | This is a required parameter. It defines the name of the attribute you want to set. |
value | This is a required parameter. It defines the value you want to give the specified attribute. |
class Person:
name = "Jerry"
age = 18
country = "India"
setattr(Person, 'age', 21)
x = getattr(Person, 'age')
print(x)
class Person:
name = "Jerry"
age = 18
country = "India"
setattr(Person, 'age', 21)
setattr(Person, 'country', "London")
x = getattr(Person,'country')
print(x)