Fonction Python hasattr()
Exemple
Vérifiez si l'objet "Person" a la propriété "age":
class Person:
name = "John"
age = 36
country = "Norway"
x = hasattr(Person, 'age')
Définition et utilisation
La hasattr()
fonction renvoie
True
si l'objet spécifié a l'attribut spécifié, sinon False
.
Syntaxe
hasattr(object, attribute)
Valeurs des paramètres
Parameter | Description |
---|---|
object | Required. An object. |
attribute | The name of the attribute you want to check if exists |
Pages connexes
La fonction delattr() , pour supprimer un attribut
La fonction getattr() , pour récupérer la valeur d'un attribut
La fonction setattr() , pour définir la valeur d'un attribut