Python setattr()

Configurare noua (How To)

Situatie

The setattr() function sets the value of the attribute of an object.

Solutie

Pasi de urmat

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:

Before modification: Adam
After modification: John

Tip solutie

Permanent

Voteaza

(7 din 14 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?