Python 2.7 如何在python中绑定静态方法?

Python 2.7 如何在python中绑定静态方法?,python-2.7,Python 2.7,我收到这个错误: 必须以实例作为第一个参数来调用unbound方法hello()(没有得到任何结果) B.py仅包含一个打印“hello”的函数“hello” 提前谢谢。oldhello()不是静态的。所以在B中,hello函数是静态地引用A的非静态oldhello。A实际上需要一个实例。我对decorator及其工作方式不太熟悉,但可以尝试在函数之前在类中声明oldholl并调用@staticmethod。我不知道如果你重写这个方法,静态是否会继续 试试这个: class B(): d

我收到这个错误:
必须以实例作为第一个参数来调用unbound方法hello()(没有得到任何结果)

B.py仅包含一个打印“hello”的函数“hello”

提前谢谢。oldhello()不是静态的。所以在B中,hello函数是静态地引用A的非静态oldhello。A实际上需要一个实例。我对decorator及其工作方式不太熟悉,但可以尝试在函数之前在类中声明oldholl并调用@staticmethod。我不知道如果你重写这个方法,静态是否会继续

试试这个:

class B():
    def hello(self):
        print "hello"

class A():
   @staticmethod
   def newHello(self):
       A.oldHello(self) # Here the error
       print ' world'

   def inject(self):
       A.oldHello = B.hello
       B.hello = A.newHello

A().inject()
B().hello()

你有没有像它所建议的那样用一个例子来试过B?你得到了什么?你是说打电话给oldHello(A())?函数hello有1个参数,需要0
def hello():
     print 'hello'
class B():
    def hello(self):
        print "hello"

class A():
   @staticmethod
   def newHello(self):
       A.oldHello(self) # Here the error
       print ' world'

   def inject(self):
       A.oldHello = B.hello
       B.hello = A.newHello

A().inject()
B().hello()