Saturday, November 14, 2020

, ,

Example program for setattr() function in Python

setattr() function in Python use to set the value of an attribute.

setattr() function
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:-

setattr() function in Python
program for setattr() function in Python


0 comments:

Post a Comment