Discuss / Python / 打卡啊,多加了一些异常判断

打卡啊,多加了一些异常判断

Topic source

# _*_ coding:utf-8 _*_

class Screen(object):

    @property

    def width(self):

        return self._width

    @width.setter

    def width(self,value):

        if not isinstance(value,(int,float)):

            raise ValueError('width must be int or float')

        if value<0:

            raise ValueError('width must be nonnegative')

        self._width=value

    @property

    def height(self):

        return self._height

    @height.setter

    def height(self,value):

        if not isinstance(value,(int,float)):

            raise ValueError('width must be int or float')

        if value<0:

            raise ValueError('width must be nonnegative')

        self._height=value

    @property 

    def resolution(self):

        return self._height*self._width


  • 1

Reply