Discuss / Python / 装饰器@property

装饰器@property

Topic source

Pklue

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

class Screen(object):

    @property

    def width(self):

        return self.ww

    @width.setter

    def width(self,w):

        if not isinstance(w,int):

            raise ValueError('数字!')

        if w < 0:

            raise ValueError('正数!')

        self.ww = w

    @property

    def height(self):

        return self.hh

    @height.setter

    def height(self, h):

        if not isinstance(h,int):

            raise ValueError('数字!')

        if h < 0:

            raise ValueError('正数!')

        self.hh = h

    @property

    def resolution(self):

        return self.ww * self.hh


  • 1

Reply