简体   繁体   中英

functools.partial as IronPython EventHandler

I'm currently trying out the IronPython interpreter. 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)

So IronPython thinks a partial object is not callable although callable(partial(print, "Changed: ")) returns 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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