Python setattr()関数
例
「person」オブジェクトの「age」プロパティの値を変更します。
class Person:
name = "John"
age = 36
country = "Norway"
setattr(Person, 'age', 40)
定義と使用法
このsetattr()
関数は、指定されたオブジェクトの指定された属性の値を設定します。
構文
setattr(object, attribute, value)
パラメータ値
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 |
関連ページ
属性を削除するためのdelattr()関数
getattr ()関数、属性の値を取得します
hasattr ()関数、属性が存在するかどうかを確認します