setattr() function in Python use to set the value of an attribute.
![]() |
| setattr() function in Python |
Syntax : -
setattr(object, name, value)
If you want to read the object then use the getattr() function.
- Parameters For setattr():-
It takes three parameters and these are:-
- Object : - the object whose value is going to be set in python program.
- Name - set the name of attribute.
- value - Desire value given to the attribute
setattr() function in python doesn't return the value.
Example program for setattr() function in Python
class Person:
name = 'Adam'
p = Person()
print('Before modification:', p.name)
# setting name to 'John'
setattr(p, 'name', 'John')
print('After modification:', p.name)Output:-

program for setattr() function in Python




