python @property装饰器 RuntimeError: maximum recursion depth exceeded

在使用@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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容