Discuss / Python / 似乎不输入width和height时调用属性s.resolution会报错,不用再进行检查了,求高人指点是不是这样-。-

似乎不输入width和height时调用属性s.resolution会报错,不用再进行检查了,求高人指点是不是这样-。-

Topic source

weareguyshot

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

class Screen(object): @property def width(self): return self._width @width.setter def width(self,width): if not isinstance(width,int): raise ValueError('width must be an integer!') if width<0 or width>10000: raise ValueError('out of range!') self._width=width

@property
def height(self):
    return self._height
@height.setter
def height(self,height):
    if not isinstance(height,int):
        raise ValueError('height must be an integer!')
    if height<0 or height>10000:
        raise ValueError('out of range!')
    self._height=height


@property
def resolution(self):
    #if self._width !=0 and self._height!=0:
        print('the resolution is %d' %(self._width*self._height))
    #else:
    #   raise ValueError('width or height haven\'t been set!')

weareguyshot

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

注释的是我之前想检查width和height是否有值时写的,后来发现没输入width和height直接s.resolution会报错:'Screen' object has no attribute '_width'这样的


  • 1

Reply