Discuss / Python / 为什么一定设setter,而不能直接通过获取后直接运算??

为什么一定设setter,而不能直接通过获取后直接运算??

Topic source

[?]

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

class Screen(object):

    def resolution(self):

        return self.width * self.height

s = Screen()

s.width = 1024

s.height = 768

print(s.resolution)

#运算结果:<bound method Screen.resolution of <__main__.Screen object at 0x00000183DAE87880>>

🍉

#2 Created at ... [Delete] [Delete and Lock User]
@property 这个是读属性的装饰器,有这个的话,会直接返回属性值


  • 1

Reply