Python setattr() Funzione
Esempio
Modificare il valore della proprietà "età" dell'oggetto "persona":
class Person:
name = "John"
age = 36
country = "Norway"
setattr(Person, 'age', 40)
Definizione e utilizzo
La setattr()
funzione imposta il valore dell'attributo specificato dell'oggetto specificato.
Sintassi
setattr(object, attribute, value)
Valori dei parametri
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 |
Pagine correlate
La funzione delattr() , per rimuovere un attributo
La funzione getattr() , per ottenere il valore di un attributo
La funzione hasattr() , per verificare se esiste un attributo