What is Jython and is it useful at all?

32,969

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

Two other reasons:

  • Embedding scripting into large Java application.
  • Use Java threads to write multi-threaded programs in Jython.

Solution 5

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.

Share:
32,969
TIMEX
Author by

TIMEX

Updated on August 25, 2020

Comments

  • TIMEX
    TIMEX almost 4 years

    I know Python, but what is Jython?

    • When will I need Jython?
    • What are the drawbacks?
    • I assume it is slow?

    Please detail it out! thanks.