How to monkeypatch a static method? [duplicate]

2019-04-27 03:26发布

This question already has an answer here:

While it's fairly simple to monkeypatch instance methods to classes, e.g.

class A(object):
    pass

def a(self):
    print "a"

A.a = a

doing this with another class's @staticmethod à la

class B(object):
    @staticmethod
    def b():
        print "static b"

A.b = B.b

results in A.b() yielding a

TypeError: unbound method b() must be called with A instance as first argument (got nothing instead)

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-27 03:57

Make A.b a static method and you should be fine:

A.b = staticmethod(B.b)

查看更多
登录 后发表回答