Updated 2014-01-20 20:14:50 by RLE

Jython is Python (2.1) written in 100% Java. It byte compiles to Java and is meant to be a scripting language for Java (it integrates very nicely with Java libraries). The idea is to write code using Python syntax to manipulate the vast Java libraries.

Compared with Jacl and TclBlend, Jython isn't so much a scripting language for Java but a different language for the JVM. Its sort of like C++ for the C/Unix API.

Why mention Jython here? Well, like Java, it too can be scripted with Tcl (see TclBlend Roux).

The funny thing is: Jython is an alternative to the Java syntax, not a scripting solution. TclBlend and Jacl for scripting Jython! Oh, my.

-- Todd Coram

Interesting stuff. Todd, please say a few more words on why Jython is not "scripting".

For me, scripting has the following values:

  1. Brevity (Succinctness) - Being able to do coarse grain things (i.e. delete files, remove directories, transfer data, manipulate the "general purpose" language in very little code.
  2. Language Constructs Geared Toward Control - If you are scripting a "general purpose" language (C, Java, etc), the scripting language should be syntatically and semantically simpler. It shouldn't fight with the language its scripting. You want to control the general purpose language's objects and artifacts, not perform a mind meld with it. You want to sling objects, not perform brain surgery on them.
  3. Rapid Prototyping - I should be able to write scripts rapidly (but still safely!).

Jython is a general purpose language (IMHO). Let's see how well it scores according to the above criteria:

  1. Brevity - Jython has some syntax shortcuts (array slicing, dictionaries, tuples, etc) but Python was never very succinct to begin with. This is not a criticism. Python was meant to be read, not scanned. Even its List Comprehensions are verbose compared to, say, Haskell. I find myself slinging classes quite a bit in Python. And that's not much briefer than Java.
  2. Language Constructs - Jython has its own rules. Data type conversion between Java and Jython is a subtle thing. Both Java and Python have distinct concepts attached to data typing. Same said for classes (and introspection on those classes). Who conforms to whom? Is Java the master and Jython the slave? Should programming Jython feel like Python or Java?
  3. Okay, this gets biased. Its similiar to item #1, but is more about how quickly I can do things with what I have. It's best represented by solving the following real problem: I have a test suite that generates a subdirectory and a bunch of files. How do I delete the files and subdirectory when I am done?

Let's look at it in Python, Jython, Tcl and Jacl:

Python:
  import os
  os.chdir(dir)
  for f in os.listdir(dir):
       os.remove(f)
  os.chdir('..')
  os.rmdir(dir)

(NEM: do you really need to change the working directory to do this in Python?)

Jython:
  import os
  os.chdir(dir)

Oops! The os file functions aren't available in Jython... We need to go to Java for this:

Jython (again):
  from java.io import File
  for f in File(dir).listFiles():
    File(str(f)).delete() # Notice str(f) is needed: Java types vs Python...
  File(dir).delete()

Tcl:
  file delete -force $dir

Jacl:
  file delete -force $dir

-- Todd Coram

snichols Interesting examples. I consider Jython to be a scripting language because it supports duck typing and you can program on the fly using Jython's interpreter. In our office there is a debate going on whether to continue to use Jython or use BeanShell instead for our functional tests. One advantage BeanShell provides for the Java developer is that they really don't need to learn a new language because BeanShell looks just like Java except it supports duck typing, which is a good thing. Duck typing is where you don't have to declare the variable first. You simply can start using it and change its type on the fly. I think all scripting languages have this.

-- RLH

Well "scripting" has been replaced with "dynamic" now. So you basically have either a "static" or a "dynamic" language. Oh and Jython is at 2.2 now.