Discuss / Python / 请问下为什么class里边的变量不加一个下划线会报错呢

请问下为什么class里边的变量不加一个下划线会报错呢

Topic source

A22B9S

#1 Created at ... [Delete] [Delete and Lock User]

class Screen(object):

    @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

  File "C:\Software\Coding learning\PythonApplication1\PythonApplication1\module5.py", line 8 in width

  File "C:\Software\Coding learning\PythonApplication1\PythonApplication1\module5.py", line 8 in width

  File "C:\Software\Coding learning\PythonApplication1\PythonApplication1\module5.py", line 8 in width

  File "C:\Software\Coding learning\PythonApplication1\PythonApplication1\module5.py", line 8 in width

  File "C:\Software\Coding learning\PythonApplication1\PythonApplication1\module5.py", line 8 in width

不理解为什么不加下划线会出现这个错误

新一仔

#3 Created at ... [Delete] [Delete and Lock User]

因為你width本身就是方法了,你怎麼又用来作変量呢

Unicode9

#4 Created at ... [Delete] [Delete and Lock User]

文中有讲解


  • 1

Reply