tags:

views:

19

answers:

1

I know that I can implement a Java interface with Jython like this:

class MyListener (Listener): 
  def foo(self, event):
    print(str(event))

Python has first-class functions so that seems like an overkill - especially for interfaces with one method. Is there a way to just pass a lambda or function which implements a single method in an interface instead?

+1  A: 

According to online examples, it is possible for the AWT/Swing Event interface. Simply create a closure with the correct arguments, pass it on and Jython should do the rest. Unfortunately I did not succeed in replicating this behavior for self declared interfaces as I always get a "TypeError: arg can't be coerced" exception.

I, too, would really like to know if it's possible and if so, what I'm doing wrong.

Zixnub
Hmm, if it's possible for AWT/Swing, it should be possible for other equivalent cases as well. I can't believe the devs would have hacked it just for certain GUI classes...
auramo