Python vs. Java

Recently I was wrestling with Google App Engine (the Java version) for what seemed like the millionth time and getting extremely frustrated by it. So I decided to download the Python version and — because I had never touched Python in my life — do a crash-course in yet another programming language.

Verdict? I love it. A few people I know say that Python is the easiest language to learn. There’s even been talk at my school about switching first-year computer science over to Python because of this; it’s apparently a no-frills language that anyone can grasp before midterms. Of course, this talk has as of yet remained just that: talk. Right now freshmen at my school continue to learn Java as I did, and other than the occasional smash-my-head-against-the-keyboard frustration I don’t blame my professors.

I’ve only been “learning” Python for about a week, which is a little over 0.5% of the time that I’ve known Java. So as you read this post, keep in mind that I am in no way qualified to say which language is better (and in truth, no one is). But here are a few of my quick observations:

Brevity

I hate things that look like this:

/**
 * Squares the provided integer. NOTE: this function does not
 * handle integer overflow. If the provided integer is too
 * large, you may get an errant result.
 *
 * @param toSquare the integer to square
 * @return the square of the provided integer
 */
public static int Square(int toSquare) {
return toSquare * toSquare;
}

Of course that was a contrived example, so here’s an actual one from UDK’s OnlineSubsystem.uc:

/**
 * Called from native code to assign the system interface
 *
 * @param NewInterface the object to assign as providing the
 * system interface
 *
 * @return TRUE if the interface is valid, FALSE otherwise
 */
event bool SetSystemInterface(Object NewInterface)
{
SystemInterface = OnlineSystemInterface(NewInterface);
// This will return false, if the interface wasn't supported
return SystemInterface != None;
}

The pet peeve I have with these things is that the code basically explains the comments, which read like a boring novel and have about as much to say as an American politician. These things are scarily common in C-syntax programming languages, and for what reason I have no idea. On the other hand, Python code style rules (to my knowledge) suggest something more like this:

# Squares x and returns the result
def square(x):
return x*x

This is because one of Python’s goals is code readability. If the code can’t tell you what it’s doing, it’s probably bad code.

Project Structure

Somehow someone decided this was a good thing:

A common Java project structure

A common Java project structure

This sort of package control is the norm in the Java industry, and what it does (besides keeping code all neat and organized) is make navigating source trees and online repositories an absolute pain.

On the other hand, so far all my python code — all 5 classes of it — has been in one file in a single manageable directory. If I wanted to I could split the classes up, maybe nest them a folder or two deep…but there’s no point. Python’s import system makes sure of that.

Ease of Use

I can run my code at the same time I’m modifying it and refresh to see the changes. It’s glorious. This is sort of like the much-touted “code as you play” feature of Unreal Engine 4, where you could start the game, hop over to Visual Studio and code, then jump back to the game and see your changes immediately. This is quite unlike Unreal Engine 3, which wants me to shut down the Editor and recompile every time I need to change half a line of code. So far I’m enjoying the freedom.

Conclusion

I like Java too much to take its sometimes numerous faults to heart, and I haven’t delved deep enough to find Python’s faults yet (and knowing programming, I can only assume there will be some). So far Python is great. Should you believe me when I say that? No. But if you’re ever in the Python neighborhood, don’t be afraid to walk around and knock on a few doors. You won’t be disappointed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s