Python 3 中的 @property 和 @staticmethod

Nyx*_*nyx 7 python oop python-3.x

我试图在一个函数上同时使用@staticmethodand ,以便结果由and not返回。@propertybar()Foo.barFoo.bar()

但是,Foo.bar返回的是<property object at 0x107e9cb30>而不是预期的 string bar

为什么@property装饰器不工作?

def do_work(str):
    return str

class Foo: 
    @property
    def foo(self):
        return do_work('foo')

    @staticmethod
    @property
    def bar():
        return do_work('bar')

f = Foo()
print(f.foo)      # foo
print(Foo.bar)    # <property object at 0x107e9cb30>
Run Code Online (Sandbox Code Playgroud)

使用Python 3.7.4