Discuss / Python / 作业打卡,关键是要理解@property和@xx.setter之间的组合关系

作业打卡,关键是要理解@property和@xx.setter之间的组合关系

Topic source

classScreen(object):

@property

defwidth(self):

returnself.__width

@width.setter

defwidth(self,value):

ifnotisinstance(value,int):

raiseValueError('width must be an int')

self.__width = value

@property

defheight(self):

returnself.__heigeht

@height.setter

defheight(self,value):

ifnotisinstance(value,int):

raiseValueError('height must be an int')

self.__height = value

@property

defresolution(self):

returnself.__width*self.__height


  • 1

Reply