Why Learn Another Language at All?

If you're a Java developer you have the benefit of working with a popular, well supported platform. There's no doubt that Java is a great skill to have in the current job market. But, if the only thing you have in your tool chest is a hammer everything looks like a nail.

There are lots of reasons to learn another language, particularly Python. In a sea of commodity Java developers you want to have skills that differentiate you. To paraphrase Paul Graham, learning Python says something about you, the developer. It says that you care enough about your chosen profession to go beyond what everyone else is doing.

Why Python?

Python can scale from developing quick utility applications to large, well architect-ed systems. Some big names use Python; Google, Red Hat, Industrial Light and Magic, NASA to name a few. Other companies prefer to keep Python their 'secret weapon'.

So what's so great about Python? Learning and using Python feels more natural than any other language I've worked with. The language is very predictable, different from the Perl mentality of there's more than one way to do it. Code is in maintenance far longer than it is in construction, so the ability to understand and re-factor code is paramount and Python excels.

Python is an object oriented scripting language. Don't get hung up on the scripting language part. Python compiles to byte code. Whenever you execute the interpreter first looks for the compiled byte code. If the interpreter can't find that, it does the compilation for you. You can even distribute your pre-compiled byte code. If you think about it, this is similar to the way it works in Java. Just imagine you had a JVM that went looking for source code to compile before throwing a ClassPathException. So it's a matter of semantics, don't think of 'scripting language' as a pejorative.

Let's look at some pseudo-code:

  >>> vowels = ['a', 'e', 'i', 'o', 'u']
  >>> name = 'Guido'
  >>> for char in name:
  ...     if char in vowels: print '%s is a vowel' % char
  ...     else: print char
  ... 
  G
  u is a vowel
  i is a vowel
  d
  o is a vowel

Okay, just kidding, that wasn't pseudo-code, that's Python. Some refer to Python as 'executable pseudo-code'. The first thing many people observe is the significant white space. Some get hung up on it because of traumatic past life experiences but don't let it get in the way. So blocks of code are delineated by indentation. This makes the very nature of the code intuitive and ends the curly brace location debate.

Analogues to Common Java Capabilities

Python has rich, intuitive data structures; lists, dictionaries and tuples. These are extremely powerful and baked into the language. Python also comes with an impressive library, the community calls this 'batteries included'. When you download Python you have everything you need to be productive.

Python comes with an interactive interpreter. If you type python on the command line you get a shell where you can execute Python code. It doesn't sound like a big deal but it's great for playing with APIs and learning the language.

There are plenty of analogues to the things you like about Java, I'll go through some of those now. Write once, run anywhere is the typical refrain when talking about Java. Without going into exhaustive detail, Python runs on the IBM mainframe, Nintendo DS and pretty much every imaginable point in between. It also runs on the Java Virtual Machine and the .NET Common Language Runtime. Python doesn't get dogmatic about the multi-platform capability. If there are capabilities specific to a platform it is very likely that Python provides an API to access them. For example Python has full COM support under Windows.

Java enjoys great IDE support. Chances are, as a Java developer, you are using Eclipse as your IDE. Eclipse has a Python plug-in called PyDEV, it has code completion, refactorings, debugger support and of course syntax hi-lighting. Of course many Python developers work with the standard VIM and Emacs.

Javadoc was a great innovation. Python has docstring. It's very simple, just use a string immediately after what you are documenting. Here's an example using the Python interpreter to declare and document a class. Then we get to the documentation programatically using a special 'doc' attribute. This is a subtle difference but it means that you don't have to hunt around for Javadocs separate from code. The documentation lives with the code and you can access the information at any time.

  >>> class Example:
  ...     'just a contrived example class'
  ...     pass
  ... 
  >>> print Example.__doc__
  just a contrived example class

I could go on and on. If you like jUnit you'll like PyUnit. If you're jazzed about the new Java annotations you'll dig Python decorators. If you're looking for a web framework see Django.

Performance is one area where Java beats Python. You have to remember that we are truly at a point where computer time is cheaper than human time. If absolute performance was an issue you might not be using Java anyways. If performance is a problem you have the option to replace Python routines with C/C++. Python code is also easier to re-architect for performance because it is more simple, has fewer lines of code and easy to understand.

To wrap it up, the best reason for you to download and learn Python right now is that the learning curve is extremely shallow.