美文网首页
python @property装饰器 RuntimeError

python @property装饰器 RuntimeError

作者: 月生鄢 | 来源:发表于2018-02-26 11:02 被阅读0次

在使用@property属性时,出现了如下错误,

class Screen(object):File "/home/eason/learncode/test/test.py", line 7, in width

    self.width=value

RuntimeError: maximum recursion depth exceeded

以下是代码:

    @property

    def width(self):

        return self.width

    @width.setter

    def width(self,value):

        self.width=value

    @property

    def height(self):

        return self.height

    @height.setter

    def height(self,value):

        self.height=value

    @property

    def resolution(self):

        return self.width*self.height

s=Screen()

s.width=1024

s.height=768

print(s.resolution)

测试了几次,发现@property装饰的方法的属性必须为一个下划线开头。即把sefl.height改成self._height

相关文章

网友评论

      本文标题:python @property装饰器 RuntimeError

      本文链接:https://www.haomeiwen.com/subject/hfpexftx.html