Digital Media Web Blogs > Web

Language musings


In the past few weeks I've been working for Derek hacking on various CD Baby projects. While I was coding up the Lucene web service in Java I started reflecting on a few words of wisdom from Andy Hertzfeld at the ETech conference a couple of years ago:

"When Java came out, I was excited -- I could write code twice as fast in Java as I could in C/C++. And with Python I can write code twice as fast as I can in Java."

That was the last straw -- I simply had to find out what this Python hype was all about. After Andy's presentation about Chandler I walked over to the conference bookstore and bought the Python in a Nutshell book to get started. I have to admit, at first I was a bit skeptical about Andy's claims. Twice as fast as Java is a tall claim -- no doubt.

While I was coding up the Lucene web service I was painfully reminded about the speed at which I can code in Java. Java is least practiced language in my toolkit of proficiencies and when I first tried my hand at Java I was impressed that I could much faster than I could in C++. Twice as fast? Perhaps when I got a solid grip on the language -- yes.

This time around, hacking on Java was a lot more painful. Having gotten used to Python over the last few months, I found myself cursing Java every time I had to do a format conversion. Why do I have to instantiate a number of objects just to convert from a String to an int? First, I have to convert to an Integer, and then I have to convert to an int -- you can't just cast from an Integer to an int, even though logically the are the same thing. Lame. As I was hacking these little annoyances kept bugging me about Java.

I hated strongly typed languages when I first had to code in Modula-II in college. I really hated it when I had to write a compiler for this asinine language, and I still don't like them. Recently, I spent a lot of time looking at Javadocs to find the right conversions between types, and I found that a large portion of the base Java API is made up of functions to convert types -- the API is literally cluttered with all these functions. Ick!

The latest Java coding bout gave me a really good perspective on the various languages in my toolkit. I've sworn off writing C/C++ unless there is very good reason to do it (read: speed). I don't think I'm going to seek out writing Java code -- after writing Python it's just not fun anymore.

I really appreciate Guido van Rossum's no-nonsense approach to language design. The language simply does what you'd expect a language to do, and it doesn't get in the way of letting you get things done. However, one of the features of Python I like the most is the Python way -- the suggested guidelines of how to code in Python. Python's idea of "One obvious way to do it" as opposed to Perl's "More than one way to do it" is more condusive to writing clear code that others can read. I appreciate that Perl aims to be a flexible tool that can be bent and twisted into a programmer's finely honed tool -- that is intensely powerful. However, unless you're coding in a cave by yourself, that is most likely not the best approach to things. In the open source world where many people may look at your code, having the code be clear, readable and expressed in the most obvious fashion is intensely valuable.

The bottom line for me is that Andy was right -- I can code twice as fast in Python as I can in Java. And I love that fact -- programming is fun again!

How would you compare Java and Python?

Categories





AddThis Social Bookmark Button



Comments (9)
Read More Entries by Robert Kaye.

9 Comments

CraigRinger said:

Strong Typing
My understanding is that like Java, but unlike C/C++, Python is strongly typed. However, unlike Java and C, Python is not statically typed. My muddled attempt at an explanation follows.

In C, I can assign a pointer to an int. The language will let me. I can typecast - not convert types, just tell the language they're something else. In perl, if I add 1 and '1', I get '2', not a compile/runtime error. My understanding is that this is weak typing.

Static typing, on the other hand, is the concept that a variable can contain only a particular type. For example, a Java 'int' variable can only contain an int.

Python is strongly, but dynamically, typed. You can't cast one object to another object - the language doesn't even have the concept. You must convert between types.

In Python a variable is only a name - a reference that points to an object. The variable its self has no type - the object it points to does. A variable can point to any object, not just one of a particular type. As such, it's strongly and dynamically typed - any variable can refer to an object of any type, but objects have a specific type that cannot be cast or changed.

In Python, the operaton:

x = '1234'
x = int(x)

instantiates a 'str' object and stores a reference to it in the name 'x'. The next statement gets the object referenced by x and passes the reference to the int.__call__ class method of the 'int' type (types in Python are actually objects too). That method parses the string and returns a reference to a new 'int' instance. That returned reference is stored in the name 'x'. 'x' has never had a type in the first place - it just went from pointing to a 'str' object to a different object that happens to be of type 'int'. No objects have changed type, they've just been created and destroyed and a reference has been changed.

Some people who know better than me:

Artima.com weblog post on the matter

Dive Into Python on Python typing

some Python.org content on the matter

aristotle said:

Strong typing revisited
There's also F#, a ML dialect for .NET from none other than MSFT Research.

bettercode said:

String to int conversion
Java: int xyz = Integer.parseInt("12324");
Python: xyz = int('12324')

so, which is easier? =8)

teejay said:

This is why I love perl
Once I tried perl I discovered I could be far more productive than other languages I had used and been taught (C,C++,Pascal,COBOL*shudder*), reverting to Java or C# is painful for any perl, python or ruby programmer.


Perl also offers the most comprehensive array of support and libraries, the community is astoundingly helpful and supportive while encouraging self-sufficiency.


Its also nice that you can build applications in perl on any platform using any tools, rather than being lost without the combination of Eclipse, ANT, a dozen Apache Jar's, a JVM, a selection of beans, etc or in the case of C# Visual Studio, several dozen commerical libraries, and another dozen commercial tools.


rmcouat said:

String to int conversion
There is no need to instantiate any objects to get from String to int, just use the static method parseInt() and invoke it using the class name

int xyz = Integer.parseInt("12324");

will do.

babelex said:

Even Better still
Having been in the same situation myself evolving through C/C++ PERL PHP Java etc.. I took a good look at Python and liked what I saw. Working with both (the ideal situation) however was not as easy, although Jython is a wonderfully there are some serious semantic divides. I have since discovered Groovy, however this also inherit some nice techniques from Ruby among other languages. Again the time savings are great and even better than python with groovy's advanced XML stuff. But the biggest win is that it lies in bed with java seamlessly, It is designed to do so. You really should take a a good look at it it might just give you the ideal solution too :)_

hullion said:

Just a little note...
Just thought I would point out that PYTHON IS STRONGLY TYPED.

luismg said:

Strong typing revisited
You can have the best of both worlds.
There's a new python-like language for the .NET framework tha uses static type inference intensively. It's called "Boo".
This way, you have the simplicity of Python with all the power and performance of C#.

There are also a couple of exciting projects aimed to make PYTHO faster than (or close to)static typed languages: PyPy, which is a python implementation in python (I know it sounds weird) coupled with improvements in type inference and just in time compilation, and Starkiller (type inferencer + c++ compiler).

aristotle said:

Strong typing revisited
Java is one of the predominant crop of languages based on ALGOL concepts — in good company with C, C++, Pascal/Delphi, Modula, and a seemingly neverending list of more obscure names. They all do many things similarly, and the concept of typing in all of those languages does suck, and pretty hard at that.

But strongly typed languages don't have to suck. And that assertion coming from a Perl wizard and zealot like me has to mean something, too. :-) The magic word is “type inferencing”. Have a read over at Strong Typing and Perl, which, despite the title has relatively little to do with Perl. I urge you to read up to the “spectacular example” at least.

Topics of Interest

Related Books

Recommended for You

Archives


 
 


Or, visit our complete archive.  

Stay Connected