On Wed, Jun 06, 2001 at 05:54:33PM +0100, Kieran Breen wrote:
| Thanks Tessa.
|
| I perhaps gave a bad example of the problem I was having.
|
| What about this:
|
| >> from javax.swing import *
| >> def foo(e):
| >> print "foo"
| >>> f = JFrame(windowListener=foo)
| Traceback (innermost last):
| File "<console>", line 1, in ?
| TypeError: can't convert <function foo at 6767586> to
| java.awt.event.WindowListener
import java
import javax
class foo( java.awt.event.WindowListener ) :
def whatever_the_method_name_is( self , event ) :
print "foo"
f = javax.swing.JFrame( windowListener=foo() )
Isn't Java fun <wink>? The problem is that on the Java side there
must be an instance that contains a bound method with a given name for
the callback mechanism to work. Even before getting there, the type
of the arguments are checked so the instance must implement the
"WindowListener" interface. This is one of the reasons that, IMNSHO,
Python far exceeds Java in quality and usability.
The "downside" of Python is that you can't instantiate an anonymous
inner class -- in fact the class can't even be anonymous. On the
upside you don't need to have a class instance in the first place!
Simply pass a function to the widget and it can simply call the
function. No mess with interfaces, adapters, and single-inheritance.
-D
|