【发布时间】:2021-09-17 15:21:47
【问题描述】:
考虑以下情况:
class test:
def foo(self, o):
print(o)
@staticmethod
def bar(o):
print(o)
@classmethod
def qux(cls, o):
print(cls)
print(o)
def baz(o):
print(o)
t = test()
class A:
meth1 = t.bar
meth2 = t.foo
meth3 = baz
meth4 = t.qux
a = A()
a.meth1()
a.meth3()
# a.meth4()
# a.meth2()
这很好用,但如果我打电话给meth2/4,我会收到以下错误:
TypeError: <foo/qux>() missing 1 required positional argument: 'o'
有什么方法可以让t.foo 和t.qux 像t.bar 和baz 一样工作?
【问题讨论】:
标签: python methods function-binding