[IronPython] Events and the += operator
Jonathan Jacobs
korpse-ironpython at kaydash.za.net
Wed Nov 23 14:05:41 CET 2005
Hi,
(I did a couple of searches on the archives and checked the bug tracker
but still found nothing directly relating to this. Hopefully I didn't
miss anything.)
I'm having a problem trying to attach an event handler to
System.Windows.Forms.Application.Idle:
>>> System.Windows.Forms.Application.Idle
<event# Idle on Application>
>>> def _(sender, e):
... print 'Zzzz'
...
>>> System.Windows.Forms.Application.Idle += _
Traceback (most recent call last):
at <shell>
TypeError: can't set attributes of built-in/extension type
'System.Windows.Forms.Application'
At first glance, it looks like this didn't work. However, creating a
Form object and passing it to Application.Run does indeed print 'Zzzz'
when idle.
Stepping through the code indicates that InPlaceAdd mimics the C# +=
syntax for events (ie. attaches the event handler) and that
AugAssignStmt always emits a "set", which correlates with CPython's
behaviour as seen below (I'm not sure whether this is part of the Python
specification or not, anyway):
>>> l = ([], [])
>>> l[0] += [1, 2]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
It would seem that IronPython needs a more Pythonic (or less C#ish, if
you prefer) way of doing this.
In the meanwhile, one can get around it by either doing:
>>> Application.Idle.__iadd__(_)
or
>>> try:
>>> Application.Idle += _
>>> except TypeError:
>>> pass
Regards
--
Jonathan
When you meet a master swordsman,
show him your sword.
When you meet a man who is not a poet,
do not show him your poem.
-- Rinzai, ninth century Zen master
More information about the Ironpython-users
mailing list