我希望这会工作:调用Python的静态方法的对象
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
找出一些东西,但它是内置的。我希望这很清楚我想在这里实现什么。
你有什么想法如何很好地做到这一点?我知道制作这些全球化的解决方案可以解决这个问题,但这在我的模块中会很混乱。
P. S. do_it_somehow
和do_it_another_way
将仅从A.do_it
被调用。
对于那些试图将这个问题标记为重复的人:我认为这个问题比引用的问题更普遍 - 这是一个描述OP在这里的同样问题的角落案例。 – jsbueno
@jsbueno:更一般的情况也包含在重复内容中。 –