Answers for "python setattr function"

2

set attribute python

setattr(object, name, value)
Posted by: Guest on April-02-2020
1

python setattr

class Person:
    name = 'Adam'
    
p = Person()
print('Before modification:', p.name)

# setting name to 'John'
setattr(p, 'name', 'John')

print('After modification:', p.name)
Posted by: Guest on March-30-2021
0

python setattr function

class Student:
    name = 'John'
 
student = Student()
 
# Set 'age' attribute to 23
setattr(student, 'age', 23)    
 
print('Student name: ', student.name)
print('Student age: ', student.age)
 
# Output
# Student name:  John
# Student age:  23
Posted by: Guest on October-09-2021

Python Answers by Framework

Browse Popular Code Answers by Language