Python: How to pass more than one argument to the property getter?

前端 未结 7 1988
清酒与你
清酒与你 2021-01-31 06:48

Consider the following example:

class A:
    @property
    def x(self): return 5

So, of course calling the a = A(); a.x will retur

7条回答
  •  面向向阳花
    2021-01-31 07:53

    I know this question is old, but, for reference, you can call your property with an argument like that:

    a = A()
    assert a.x == 5
    assert A.x.fget(a, True) == -5
    

    As other mentioned by others, this is not advised.

提交回复
热议问题