Discuss / Python / 记录-使用@property

记录-使用@property

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('width must be an integer!')
        self._width = value

    @property    def height(self):
        return self._height

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

    @property    def resolution(self):
        self._resolution = self._width*self._height
        return self._resolution

Myscreen = Screen()
Myscreen.width = 1024Myscreen.height = 768print('Myscreen screen solution is ', Myscreen.resolution)

  • 1

Reply