0
0

Pythonのセッターゲッター

Posted at

Pythonのゲッターとセッターの基本の書き方

python set_get.py
class TestProperty:
    def __init__(self, val):
        self.val = val  # セッターを使用

    @property
    def val(self):
        return self.__val

    @val.setter
    def val(self, val):
        self.__val = val

# main
obj = TestProperty(100)
print(obj.val) # ゲッターを使用
obj.val = 20
print(obj.val)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0