How to set attributes using property decorators?

前端 未结 1 508
悲哀的现实
悲哀的现实 2020-12-08 01:57

This code returns an error: AttributeError: can\'t set attribute This is really a pity because I would like to use properties instead of calling the methods. Does anyone kno

相关标签:
1条回答
  • 2020-12-08 02:24

    Is this what you want?

    class C(object):
        def __init__(self):
            self._x = None
    
        @property
        def x(self):
            """I'm the 'x' property."""
            return self._x
    
        @x.setter
        def x(self, value):
            self._x = value
    

    Taken from http://docs.python.org/library/functions.html#property.

    0 讨论(0)
提交回复
热议问题