setattr() is used to assign the object attribute its value. Apart from ways to assign values to class variables, through constructors and object functions, this method gives you an alternative way to assign value.
Syntax : setattr(obj, var, val)
obj : Object whose which attribute is to be assigned.
var : object attribute which has to be assigned.
val : value with which variable is to be assigned.
Code #1 : demonstrating working of setattr()
Before setattr name : GeeksforGeeks After setattr name : Geeks4Geeks
- setattr() can be used to assign None to any object attribute.
- setattr() can be used to initialize a new object attribute.
Code #2 : demonstrating properties of setattr()
Before setattr name : GeeksforGeeks After setattr name : None After setattr description : CS portal
- class method vs static method in Python
- Python | next() method
- Python | set() method
- Python | hash() method
- Python Dictionary | pop() method
- Python | Tensorflow exp() method
- Python | os.chmod method
- Python | iter() method
- Python | PyTorch sin() method
- Python | Tensorflow log() method
- Python | after method in Tkinter
- Python | os.chflags() method
- Python | object() method
- Python | bytes() method
- Python | PyTorch tan() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.