Discuss / Python / 久违的作业,可是又和大家的好不一样

久违的作业,可是又和大家的好不一样

Topic source

为什么和大家的这么不一样啊,虽然输出是对的但是总感觉怪怪的 而且我发现我加的isinstance判断也没用,试过输入字符,但是报错不是我定的内容 求大神解释啊啊啊啊啊啊(゚皿゚メ)
说实话这一章根本没看太明白,再加上之前就没看明白的装饰器,简直雪崩……


class Screen(object):
    @property
    def resolution(self):
        return self.width*self.height

    @resolution.setter
    def resolution(self,width,height):
        if not isinstance(width,int):
            raise ValueError('width must be an integer!')
        elif not isinstance(height,int):
            raise ValueError('height must be an integer!')
        else:
            self._width=width
            self._height=height

s=Screen()
s.width=1024
s.height=768
print('The resolution of this screen is:',s.resolution)

你的代码就相当于 class Student():

def resolution(self):
return self.width*self.height

不信,你试试。


  • 1

Reply