How does the @property decorator work in Python?

后端 未结 13 2079
闹比i
闹比i 2020-11-21 04:49

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only

13条回答
  •  名媛妹妹
    2020-11-21 05:33

    Documentation says it's just a shortcut for creating readonly properties. So

    @property
    def x(self):
        return self._x
    

    is equivalent to

    def getx(self):
        return self._x
    x = property(getx)
    

提交回复
热议问题