views:

648

answers:

9

I know Python. When will I need Jython? What are the drawbacks. I assume it is slow? Please detail it out! thanks.

+9  A: 

Quoting Wikipedia:

Jython, successor of JPython, is an implementation of the Python programming language written in Java.

As for what uses it may have:

Jython programs can seamlessly import and use any Java class. Except for some standard modules, Jython programs use Java classes instead of Python modules.

Performance measurements of Jython, IronPython and CPython show that both Jython and IronPython outperform CPython for some cases with large datasets.

Joey
Jython doesn't compile to bytecode the same way Java does. The bytecode does all the wonderful dynamic runtime things that CPython does, so is considerably slower than Java.
orip
+4  A: 

Jython is a Python implementation which runs on top of JVM. Theoretically, Jython is a bit slower, then CPython.

You can find some benchmarks here: http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/

Vitaliy Liptchinsky
The followup article is interesting too: http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-hammers-cpython-when-not-mutating-class-attributes/
dangph
Now we know that Jython is asymptotically faster than CPython in certain benchmarks.
kaizer.se
A: 

The JVM does some nice dynamic optimization, but it's probably nothing to get excited about. JRuby sometimes runs faster on the JVM (http://www.engineyard.com/blog/2009/j-is-for-jvm-why-the-j-in-jruby/) by optimizing the code paths that run a lot, but that's Ruby, not Python.

Java libraries are the main reason. Lots of companies have big globs of Java code that can be scripted with Python. There's also a few open source frameworks (Struts and Eclipse spring to mind) which you can script with Jython.

wisty
+2  A: 

When will I need Jython?

When you want to program in Python but need (or want) to have the result run on a Java virtual machine, or use existing Java components.

What are the drawbacks.

Jython may not be 100% compatible with Python, though any incompatibility would be considered a bug. If you later want/need to run on CPython, any code that uses Java components will have to be rewritten.

I assume it is slow?

That depends, as always, on your specific usecase. It may actually be faster than CPython in some cases; and of course it depends on the specific JVM you run under - these get better all the time.

Michael Borgwardt
+3  A: 

When will I need Jython?

For example to add a nice scripting language to your code.

What are the drawbacks?

The main drawback is that Jython lags behind the official CPython distribution. Currently, you can get a version of Jython that is compatible with Python 2.5.2 while CPython is at 3.1.

Also some esoteric modules aren't supported. Usually, you won't notice and/or be able to easily find a Java replacement.

I assume it is slow?

Compared to what? Usually, it's either fast enough or, when it isn't, you can replace a few lines of Python with about 1'000 lines of much faster Java.

Aaron Digulla
+2  A: 

Two other reasons:

  • Embedding scripting into large Java application.
  • Use Java threads to write multi-threaded programs in Jython.
Richie
+1  A: 

When will I need Jython?

You can write administrative scripts for Websphere application server using Jython. The scripts are used to automate common administrative tasks and configure security in Websphere application server.

WebSphere utilizes the JMX API to create MBeans that are exposed as an API of internal WebSphere objects. As the Jython scripts are based on Java and they run on the JVM they can access those objects and can be utilized to configure WebSphere.

Besides it is fun to write code in Jython and you learn a new language.

Here is the details

Priyabrata Hota
A: 

When will I need Jython?

I need Jython to test JDBC drivers. Some of apps I use work use ODBC, some use JDBC. Using Jython I can test both type of drivers from one Python source (to test ODBC I use JDBC-ODBC bridge).

Michał Niklas
A: 

If you know Python and has bought into the "pythonic" way of doing things, then Jython allows you to bring that philosophy to the JVM stack. If you do this, it is much more than just adding scripting capability.

In our latest projects, all the custom and business logic is built in Jython, at the same time we can still leverage some of the great tried and tested Java libraries like Solr, Jasperreports, Quartz, Jetty, Velocity to name a few.

It does get compiled to bytecode, however, an extra layer is being added, but is no different to using an ORM instead of straight JDBC for example.

What you gain in productivity far out weighs the minuscule lost in performance.

On the server side, Jython is rarely the bottleneck. For mini desktop apps, there may be issues, but very much dependent on what you are trying to do.

The latest JDK, together with containers like Jetty or Tomcat are very mature and stable, adding Python on top, in many cases, gives the best of both worlds.

Ron