Discuss / Python / class Screen(object):

class Screen(object):

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!')
        if value < 0 :
            raise ValueError('score must big than 0 !')
        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!')
        if value < 0 :
            raise ValueError('score must big than 0 !')
        self._height = value
    @property
    def resolution(self):
        return self._height * self._width

请问一下,

 def width(self, value):

width里的变量名可以也定义成width,而不是value吗?这个地方有没有什么写法的内在要求(比如函数名和变量名不能重复)?求大神解答

lostexin

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

形参只要符合命名规则就可以了,比如不和系统关键字和保留字冲突、命名正确。形参和函数名相同没什么问题


  • 1

Reply