Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
(More Than) Five Things You Didn't Know You Could Do With Python
By Cameron Laird

Rate This Article: Add This Article To:

(More Than) Five Things You Didn't Know You Could Do With Python - ' Better Java Than Java '
( Page 3 of 5 )

A Better Java than Java

One habit we have with customers is to say that the decision facing a particular project is not between Python and some other language, but what proportions to use of each. There are choices besides "all" and "nothing." Most of our contracts end up implemented in a mixture of languages, because that turns out to be the most cost-effective way to achieve the reliability and functionality we deliver.

Python and Java make for particularly interesting contrasts and combinations. Although Java has a reputation as the paramount "enterprise" language, our experience is that Python actually "scales" and promotes teamwork better than Java does. One effective way to exploit the strengths of each is to use Jython to script and develop applications based on Java class libraries.

Jython is an implementation of the Python syntax and semantics in Java, rather than the C in which the standard Python distribution (sometimes called, "CPython") is written. Jython was originally the creation of the same Jim Hugunin now leading the IronPython project; Jython is available from the Jython Developers on equivalently liberal terms to those of CPython.

Jython is an alternative to Java debuggers, in that the interpreter allows full interaction with the underlying JVM. Just as mentioned above for COM, it's very convenient to experiment. This is particularly handy, for example, with graphical user interface (GUI) applications. To assess the appearance of different widget configurations in a conventional Java development environment might involve several minutes for each recompilation, or advanced debugger techniques. With Jython, though you can interactively change a few instance variables and immediately see the effects.

As with all flavors of Python, once you interactively determine the APIs and sequences pertinent to your problem, you can create a "batch" script with all the same commands. There are no limits on Python "scripts." Although that word suggests to some a small, sloppily-written code fragment, Python is a fully object-oriented, strictly (if dynamically) typed language, whether used batch or interactively.

In comparison to Java, Jython is admirably concise and productive. Stephen Ferg, in a well-referenced write-up of the two languages, illustrates the contrast in their concise expressiveness with these two (slightly adapted) examples of counting to ten:

      // This is Java source.
      public class CountUp
      {
        public static void main(String[] args)
	{
	  // print the integers from 1 to 10
          for (int i = 1; i <= 10; i++) {
             System.out.println(i);
          }
        }
      }
     

      # This is Python source.
      # print the integers from 1 to 10
      for i in range(1,11):
          print i

This is an unfair comparison, of course. main and I/O are two particularly verbose elements of Java, and it would be wrong to judge the whole language just on them.

On the other hand, many programmers who work with both languages say that the ratio of line-count above underestimates Python's advantage. Ferg is one of several developers who have documented that their experience leads them to expect Java to be from two to ten (!) times as wordy as Python.

Remember: with Jython you get the best of both worlds.



 
 
>>> More Microsoft Languages Articles          >>> More By Cameron Laird