Discuss / Python / 请教,这种报错的原因是什么?

请教,这种报错的原因是什么?

Topic source
    @property
    def width(slef):
        return slef.width 
        
        
    @width.setter
    def width (self,width):
            self.width=width
    @property
    def height(slef):
        return slef.height
    @height .setter
    def height (self,height):
        self.height=height
    @property
    def resolution(self):
            return self.height*slef.width

为什么我会出现下面的报错,应该怎么修改,网上查是归递超出上限,??哪里是归递

Traceback (most recent call last): 

  File "C:\Users\ADMINI~1\AppData\Local\Temp\learn_python_ha4r1d8f_py\test_47.py", line 22, in <module> 

    s.width = 1024 

  File "C:\Users\ADMINI~1\AppData\Local\Temp\learn_python_ha4r1d8f_py\test_47.py", line 10, in width 

    self.width=width 

  File "C:\Users\ADMINI~1\AppData\Local\Temp\learn_python_ha4r1d8f_py\test_47.py", line 10, in width 

    self.width=width 

  File "C:\Users\ADMINI~1\AppData\Local\Temp\learn_python_ha4r1d8f_py\test_47.py", line 10, in width 

    self.width=width 

  [Previous line repeated 995 more times] 

RecursionError: maximum recursion depth exceeded

    def height(slef):
        return slef.height

您这self都拼错了呀

width就是一个函数名,相当于c的指针,是一个地址,将width传给self.width之后,并没有结束,而是又执行了一遍width函数,一直在width函数里面,是一个死循环,你把width参数改成不和width函数重名的变量就行

@property 

def width(self):

  return self.value

@width.setter

def width(self,value):

  self.width=value


BlockBoss0_o

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

class内部访问属性,需要加下划线  :

class Screen(object):    @property    def width(self):        return self._width    @width.setter    def width(self, width):        self._width = width    @property    def height(self):        return self._height    @height.setter    def height(self, height):        self._height = height    @property    def resolution(self):        return self._width * self._height

  • 1

Reply