开发者

What is Jython and is it useful at all? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 3 years ago.

Improve this question

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! than开发者_如何学Goks.


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.


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.


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.


Two other reasons:

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


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/


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.


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


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).


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.


To achieve a good speed performance or implementing a real multithreading program, calling python script directly from java (native) is the best way. Just prepare your python script then let java do the rest for concurrent invocation into your python script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜