Speak and Shout

Wednesday, January 04, 2006

Jython experiments, pt 2

I had a little time to play with both Java and Jython at work today during the down time before our software release. I typed up a couple of Swing example programs from my Java programming book. One was simply a JFrame with a few widgets; the other was a JColorChooser that changed the foreground color of a widget.

I was using the latest Eclipse with the JyDT plugin, so once I had the Java programs running, I could split the editor window and type in the Jython equivalent underneath.

I began to notice something.

The Jython code wasn't any shorter than the Java code.

I had to create inner classes (derived from ActionListener and WindowListener) to handle events. However, Java can create anonymous classes; Jython can't. The other Java win was the ability to create final variables that could be seen inside the inner class. With Jython, I had to use a custom __init__ method to pass the variables in. All this was slow going, as it dawned on me after some Googling on the subject that I really didn't have any other options.

The other obvious thing I noticed was that Eclipse was helping me everywhere with my Java code. There's a syntax error here. You're using a class you didn't import. You didn't implement a method you were supposed to. Eclipse seemed to courteously nudge me toward my goal.

JyDT ... nothing. Stony silence. Well, no, after I typed "from javax.swing import *" actually it did report a syntax error, but ... it was wrong. Reading the FAQ told me that JyDT can't parse this type of import correctly; it's a known bug. Hm.

Once I was done with my programs, I sat back in my chair and thought for a minute. I was startled to realize that Java+Eclipse pretty much took away all of Jython's advantages for my small sample set.

I know, my comparison is limited and, therefore, totally unfair. But who has time to waste on languages/tools that are painful to work with?

Bye bye, Jython.

Update: I got schooled. Phillip J. Eby and another Anonymous poster pointed out in the comments that Jython does indeed have anonymous classes and that I shouldn't be writing Jython code the same way that I write Java code. I will go back, study some provided Jython examples, and give it another go.

4 Comments:

  • """Java can create anonymous classes; Jython can't."""

    Are you sure? Have you tried this:

    def make_listener(func):
        class Listener(SomeListener): pass
        Listener.some_method = func
        return Listener()

    SomethingNeedingAListener.foo(
        lambda self,event: otherthing.do(event)
    )

    If you write code in Jython the same way you do in Java, you're not going to see any benefits.

    By Blogger PJE, At 9:51 AM  

  • Are you sure? Have you tried this:

    No, I'm not sure. My experience is based on googling for 10 minutes for the answer, which is about my upper limit for search time.

    Unfortunately, I can't make sense of your example. Probably my lack of experience. Can you point me to a Jython Swing example that will school me?

    By Blogger Brandon Corfman, At 12:46 PM  

  • Though i'm not a jython/swing guy, the jython mailing lists ought to be useful for digging up tips and sample code. Just picking some examples out of there, perhaps

    this
    or this
    or the code linked from
    here
    would be helpful.

    By Anonymous Anonymous, At 2:32 PM  

  • I don't know how well the two mix, but it occurs to me that for the UI layer, regular Java might be in order (to get the Eclipse help). The Python code really comes into its own when you really benefit from the native structures (dictionaries and lists). But I am not sure how all that plays out... when trying to mix that stuff.

    By Anonymous ScW, At 10:55 AM  

Post a Comment



<$I18N$LinksToThisPost>:

Create a Link

<< Home