How to monkeypatch a static method? [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-07 01:31:02

问题


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:


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

A.b = staticmethod(B.b)



来源:https://stackoverflow.com/questions/18701102/how-to-monkeypatch-a-static-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!