【发布时间】:2014-03-09 05:49:34
【问题描述】:
我希望这会奏效:
class A(object):
@classmethod
def do_it(cls, spam, eggs):
if spam in A.ways_to_do_it:
A.ways_to_do_it[spam](eggs)
super(A, cls).do_it(spam, eggs)
@staticmethod
def do_it_somehow(eggs):
...
@staticmethod
def do_it_another_way(eggs):
...
ways_to_do_it = {
'somehow': do_it_somehow,
'another_way': do_it_another_way,
}
但它引发了TypeError: 'staticmethod' object is not callable。我想检查staticmethod 以找出一些东西,但它是内置的。我希望很清楚我想在这里实现什么。
你有什么想法可以做得很好吗?我知道将这些 @staticmethods 设为全局可以解决问题,但这会在我的模块中造成混乱。
P。 S. do_it_somehow 和 do_it_another_way 将仅从 A.do_it 调用。
【问题讨论】:
-
至于那些试图将这个问题标记为重复的人:我认为这个问题比引用的问题更普遍——这是一个极端情况,描述了 OP 在此处遇到的相同问题。
-
@jsbueno:更一般的情况也包含在重复中。
标签: python function object static-methods class-method