简体   繁体   English

functools.partial作为IronPython EventHandler

[英]functools.partial as IronPython EventHandler

I'm currently trying out the IronPython interpreter. 我正在尝试使用IronPython解释器。 While doing the Tutorial i came across delegates and event handlers. 在做教程时,遇到了代理和事件处理程序。 The tutorial does something like this: 教程做了这样的事情:

from System.IO import FileSystemWatcher

w = FileSystemWatcher()

def handle(*args):
    print args

w.Changed += handle

So i tried to be smart and do this: 所以我试图聪明并做到这一点:

from System.IO import FileSystemWatcher
from __future__ import print_function
from functools import partial    

w = FileSystemWatcher()
w.Changed += partial(print, "Changed: ")

Which failed with: 哪个失败了:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Object is not callable.

Where line 1 refers to the last line in the (interactive session) line 1是指(交互式会话)中的最后一行

So IronPython thinks a partial object is not callable although callable(partial(print, "Changed: ")) returns True 所以IronPython认为partial对象不可调用,尽管callable(partial(print, "Changed: "))返回True

With this workaround the handler is accepted: 通过此解决方法,接受处理程序:

w.Changed += partial(print, "Changed: ").__call__

My question: 我的问题:

Why is a partial object not accepted as an event handler. 为什么不接受partial对象作为事件处理程序。 Is this a bug? 这是一个错误吗?

This is probably not solution, one could expect, but there is an issue, opened for couple years now - https://github.com/IronLanguages/main/issues/808 这可能不是解决方案,人们可​​以预期,但是有一个问题,现在开了几年 - 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 在.NET版本的2.6.2和2.7b1中不起作用:4.0.30319.1 ipy26 testcase-26482.py

Object is not callable. 对象不可调用。

ipy27 testcase-26482.py ipy27 testcase-26482.py

Object is not callable.py 对象不是callable.py

testcase-26482.py testcase-26482.py

Object is not callable. 对象不可调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM