Discuss / Python / 为什么property修饰的setter只能修改一个参数? 如何才能修改self.height?

为什么property修饰的setter只能修改一个参数? 如何才能修改self.height?

Topic source

Carbon

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

class Screen(object):

    @property  # 对resolution方法进行修饰,该函数变成了实例对象的属性

    def resolution(self):

        return self.width * self.height

    @resolution.setter  # 对resolution方法进行修饰,该函数变成了实例对象的属性

    def resolution(self, width):

        self.width = width

# 测试:

s = Screen()

s.width = 1024

s.height = 768

print('resolution =', s.resolution)

if s.resolution == 786432:

    print('测试通过!')

else:

    print('测试失败!')

s.resolution = 15

print(s.resolution)  # 11520  15*768


  • 1

Reply