Python hasattr()関数
例
「Person」オブジェクトに「age」プロパティがあるかどうかを確認します。
class Person:
name = "John"
age = 36
country = "Norway"
x = hasattr(Person, 'age')
定義と使用法
指定されたオブジェクトに指定された属性がある場合、関数は戻ります。それ以外の場合hasattr()
は
。True
False
構文
hasattr(object, attribute)
パラメータ値
Parameter | Description |
---|---|
object | Required. An object. |
attribute | The name of the attribute you want to check if exists |
関連ページ
属性を削除するためのdelattr()関数
getattr ()関数、属性の値を取得します
属性の値を設定するためのsetattr()関数