Python hasattr() Funzione
Esempio
Verifica se l'oggetto "Persona" ha la proprietà "età":
class Person:
name = "John"
age = 36
country = "Norway"
x = hasattr(Person, 'age')
Definizione e utilizzo
La hasattr()
funzione restituisce
True
se l'oggetto specificato ha l'attributo specificato, altrimenti False
.
Sintassi
hasattr(object, attribute)
Valori dei parametri
Parameter | Description |
---|---|
object | Required. An object. |
attribute | The name of the attribute you want to check if exists |
Pagine correlate
La funzione delattr() , per rimuovere un attributo
La funzione getattr() , per ottenere il valore di un attributo
La funzione setattr() , per impostare il valore di un attributo