
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What does setattr() function do in Python?
The setattr() method
The setattr() method sets the value of given attribute of an object.
Syntax
The syntax of setattr() method is −
setattr(object, name, value)
The setattr() method takes three parameters −
The setattr() method returns None.
Example
class Male: name = 'Abel' x = Male() print('Before modification:', x.name) # setting name to 'Jason' setattr(x, 'name', 'Jason') print('After modification:', x.name)
Output
This gives the output
('Before modification:', 'Abel') ('After modification:', 'Jason')
- Related Articles
- What does delattr() function do in Python?\n\n
- What does built-in class attribute __name__ do in Python?\\\\n\\\\n
- What does reload() function do in Python?
- What does raw_input() function do in python?
- What does input() function do in python?
- What does print() function do in Python?
- What does open() function do in Python?
- What does close() function do in Python?
- What does os.pipe() function do in Python?
- What does getattr() function do in Python?
- What does hasattr() function do in Python?
- What does the method listIterator(n) do in java?
- What is difference in getattr() and setattr() functions in Python?
- What does the repr() function do in Python Object Oriented Programming?
- What does the str() function do in Python Object Oriented Programming?

Advertisements