先来段官方文档压压惊。。
property(fget=None, fset=None, fdel=None, doc=None)

Return a property attribute.

fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del’ing, an attribute. Typical use is to define a managed attribute x:

class C:
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x
    def setx(self, value):
        self._x = value
    def delx(self):
        del self._x
    x = property(getx, setx, delx, "I'm the 'x' property.")

If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.

 

property的用法是为你的类设置一个属性值。第一个参数设定获取属性值的方法,第二个参数设定设置这个属性值的方法,第三个参数设定删除这个属性值的方法,第四个参数是文档。c.x会调用第一个参数的方法,c.x = value 调用第二个方法,del c.x调用第三个方法。这样

相关文章:

  • 2021-12-04
  • 2022-01-19
  • 2021-10-16
  • 2021-06-24
  • 2021-08-27
  • 2021-07-22
猜你喜欢
  • 2021-12-04
  • 2021-07-06
  • 2021-04-24
  • 2021-12-04
  • 2021-09-17
  • 2021-12-09
相关资源
相似解决方案