Python setattr() Functie
Voorbeeld
Verander de waarde van de "age" eigenschap van het "person" object:
class Person:
name = "John"
age = 36
country = "Norway"
setattr(Person, 'age', 40)
Definitie en gebruik
De setattr()
functie stelt de waarde in van het gespecificeerde attribuut van het gespecificeerde object.
Syntaxis
setattr(object, attribute, value)
Parameterwaarden
Parameter | Description |
---|---|
object | Required. An object. |
attribute | Required. The name of the attribute you want to set |
value | Required. The value you want to give the specified attribute |
Gerelateerde pagina's
De functie delattr() om een attribuut te verwijderen
De functie getattr() om de waarde van een attribuut te krijgen
De hasattr() functie, om te controleren of een attribuut bestaat