我目前正在试用 IronPython
解释器。在学习教程时,我遇到了委托(delegate)和事件处理程序。本教程执行如下操作:
from System.IO import FileSystemWatcher
w = FileSystemWatcher()
def handle(*args):
print args
w.Changed += handle
所以我试着变得聪明并这样做:
from System.IO import FileSystemWatcher
from __future__ import print_function
from functools import partial
w = FileSystemWatcher()
w.Changed += partial(print, "Changed: ")
哪个失败了:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Object is not callable.
第 1 行
指的是(交互式 session )中的最后一行
因此 IronPython 认为 partial
对象不可调用,尽管 callable(partial(print, "Changed: "))
返回 True
使用此解决方法,可以接受处理程序:
w.Changed += partial(print, "Changed: ").__call__
我的问题:
为什么partial
对象不被接受为事件处理程序。这是错误吗?
最佳答案
这可能不是解决方案,人们可以预料,但有一个问题,现在已经开放了几年 - https://github.com/IronLanguages/main/issues/808
Doesn't work in 2.6.2 and 2.7b1 on .NET Version: 4.0.30319.1 ipy26 testcase-26482.py
Object is not callable.
ipy27 testcase-26482.py
Object is not callable.py
testcase-26482.py
Object is not callable.
关于python - functools.partial 作为 IronPython 事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574401/