在 python 中传递静态方法
Passing static methods in python
您能解释一下为什么以下代码片段不起作用吗?
class A:
@staticmethod
def f():
print('A.f')
dict = {'f': f}
def callMe(g):
g()
callMe(A.dict['f'])
它产生
TypeError: 'staticmethod' object is not callable
有趣的是,将其更改为
class A:
@staticmethod
def f():
print('A.f')
dict = {'f': f}
def callMe(g):
g()
callMe(A.f)
或到
class A:
@staticmethod
def f():
print('A.f')
dict = {'f': lambda: A.f()}
def callMe(g):
g()
callMe(A.dict['f'])
给出了预期的结果
A.f
据我所知,Python 2 和 3 中的行为是相同的。
A
中的 f
对象是一个 descriptor,而不是静态方法本身——它 returns使用 A 的实例调用时的静态方法;阅读 link,并查找 "descriptor protocol" 以了解有关其工作原理的更多信息。该方法本身存储为描述符的 __func__
属性。
你可以自己看看:
>>> A.f
<function A.f at 0x7fa8acc7ca60>
>>> A.__dict__['f']
<staticmethod object at 0x7fa8acc990b8>
>>> A.__dict__['f'].__func__ # The stored method
<function A.f at 0x7fa8acc7ca60>
>>> A.__dict__['f'].__get__(A) # This is (kinda) what happens when you run A.f
<function A.f at 0x7fa8acc7ca60>
另请注意,您可以使用A.__dict__
来访问f
描述符对象,您不需要制作自己的字典来存储它。
staticmethod 对象是 descriptor, and you need to access it as an attribute (of the class) for the descriptor mechanism to take effect. The staticmethod object itself is not callable, but the result of its __get__
is callable. See also this Python bug discussion.
您能解释一下为什么以下代码片段不起作用吗?
class A:
@staticmethod
def f():
print('A.f')
dict = {'f': f}
def callMe(g):
g()
callMe(A.dict['f'])
它产生
TypeError: 'staticmethod' object is not callable
有趣的是,将其更改为
class A:
@staticmethod
def f():
print('A.f')
dict = {'f': f}
def callMe(g):
g()
callMe(A.f)
或到
class A:
@staticmethod
def f():
print('A.f')
dict = {'f': lambda: A.f()}
def callMe(g):
g()
callMe(A.dict['f'])
给出了预期的结果
A.f
据我所知,Python 2 和 3 中的行为是相同的。
A
中的 f
对象是一个 descriptor,而不是静态方法本身——它 returns使用 A 的实例调用时的静态方法;阅读 link,并查找 "descriptor protocol" 以了解有关其工作原理的更多信息。该方法本身存储为描述符的 __func__
属性。
你可以自己看看:
>>> A.f
<function A.f at 0x7fa8acc7ca60>
>>> A.__dict__['f']
<staticmethod object at 0x7fa8acc990b8>
>>> A.__dict__['f'].__func__ # The stored method
<function A.f at 0x7fa8acc7ca60>
>>> A.__dict__['f'].__get__(A) # This is (kinda) what happens when you run A.f
<function A.f at 0x7fa8acc7ca60>
另请注意,您可以使用A.__dict__
来访问f
描述符对象,您不需要制作自己的字典来存储它。
staticmethod 对象是 descriptor, and you need to access it as an attribute (of the class) for the descriptor mechanism to take effect. The staticmethod object itself is not callable, but the result of its __get__
is callable. See also this Python bug discussion.