Python: setattr() function
setattr() function
The setattr() function is used to set the value of the specified attribute of the specified object.
Version:
(Python 3.2.5)
Syntax:
setattr(object, name, value)
Parameter:
Name | Description | Required / Optional |
---|---|---|
object | An object. | Required. |
name | The name of the attribute to be set. | Required. |
value | The value you want to give the specified attribute | Required. |
Example: Python setattr() function
class Person:
name = 'Bishop'
p = Person()
print('Before modification:', p.name)
# setting name to 'David'
setattr(p, 'name', 'David')
print('After modification:', p.name)
Output:
Before modification: Bishop After modification: David
Python Code Editor:
Test your Python skills with w3resource's quiz
Python: Tips of the Day
How do I check if a list is empty?
For example, if passed the following:
a = [] if not a: print("List is empty")
Ref: https://bit.ly/2A4JXx9
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework