Discuss / Python / 报错:属性不存在,明明有啊

报错:属性不存在,明明有啊

Topic source

class Screen(object): @property def width(self): return self.width @width.setter def width(self,value): if not isinstance(value,int): raise ValueError('TypeError') self.width=value

@property
def height(self):
    return self.__height
@height.setter
def height(self,value):
    if not isinstance(value,int):
        raise ValueError('TypeError')
    self.__height=value

@property
def resolution(self):
    return self.__width*self.__height

s=Screen() s.width=1024 s.heigth=768 print(s.resolution)

Traceback (most recent call last): File "C:/Users/Administrator/Desktop/11.py", line 26, in <module> print(s.resolution) File "C:/Users/Administrator/Desktop/11.py", line 22, in resolution return self.width*self.height AttributeError: 'Screen' object has no attribute '_Screen__height'

self.width 少了__

狐狸leslie

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

这个加__是什么意思啊?为什么加了就没问题了呢?


  • 1

Reply