我知道这种情况很奇怪,但不知何故我遇到了:
class foo
#static method
@staticmethod
def test():
pass
# class variable
c = {'name' : <i want to reference test method here.>}
怎么做呢?
仅供参考:
我认为这应该被视为Python的最差实践。如果可以的话,使用静态方法并不是Python的方式...
我知道这种情况很奇怪,但不知何故我遇到了:
class foo
#static method
@staticmethod
def test():
pass
# class variable
c = {'name' : <i want to reference test method here.>}
怎么做呢?
仅供参考:
我认为这应该被视为Python的最差实践。如果可以的话,使用静态方法并不是Python的方式...
class Foo:
# static method
@staticmethod
def test():
pass
# class variable
c = {'name' : test }
class Foo:
# static method
@staticmethod
def test():
pass
# class variable
c = {'name' : test }
Foo.c['name']
是描述符对象,因此不可调用。您需要键入 Foo.c['name'].__get__(None, Foo)()
来正确调用此处的 test()
。如果您不熟悉Python中的描述符,请查看术语表,并且网上有大量文档可供参考。此外,请查看此线程,该线程似乎接近您的用例。c
类属性。class Foo(object):
@staticmethod
def test():
pass
Foo.c = {'name': Foo.test}
__metaclass__
的文档。
staticmethod
。Python有普通函数可用于此应用。 - Mike Graham