Python vs. Java: Readability

This node will address only one area of comparison between programming languages: readability. Do not, however, think that this sort of comparison is superficial and not valuable. Some estimates put code maintenence at almost fifty percent of total cost in a development project. Going back to code you wrote months ago, it can often be quite difficult to remember what's going on in your program. This is especially true if the code was written by another person. While good comments will always help, the truth remains: ugly code is hard to work with.

Readability is an area in which Python excels. To help give you an idea of this, I've written several short programming exercises in both Java and Python. Now, to be fair, I'm not really comparing apples to apples here. Java is certainly the more powerful language, and one would expect it to be more complex. However, Java's syntax and formatting is similar to that of C and C++. I would eventually like to flesh this node out with examples comparing Python to Perl, but alas, my ability with Perl is not sufficient at this time. Until then, I give you the samples below:


Console Test:

Java:
public class ConsoleTest {
    public static void main(String[] args) {
        for (int i = 0; i < 1000000; i++) {
            System.out.println(i);
        }
    }
}

Python:

for x in xrange(1000000):
    print x



Hash Test:

Java:
import java.util.Hashtable;

public class HashTest {
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++ ) {
            Hashtable x = new Hashtable();
            for (int j = 0; j < 1000; j++) {
                x.put(new Integer(i), new Integer(j));
                x.get(new Integer(i));
            }
        }
    }
}

Python:

for i in xrange(1000):
    x={}
    for j in xrange(1000):
        x[j]=i
        x[j]



List Test:

Java:
import java.util.Vector;

public class ListTest {
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            Vector v = new Vector();
            v.addElement("a");
            v.addElement("b");
            v.addElement("c");
            v.addElement("d");
            v.addElement("e");
            v.addElement("f");
            v.addElement("g");
            for (int j = 0; j < 1000; j++) {
                v.addElement(new Integer(j));
                v.elementAt(j);
            }
        }
    }
}

Python:

for i in xrange(1000):
    v=['a','b','c','d','e','f','g']
    for j in xrange(1000):
        v.append(j)
        v[j]



IO Test:

Java:
import java.io.*;

public class IOTest
{
    public static void main(String[] args) {
        try {
            File f = new File("scratch");
            PrintWriter ps = new PrintWriter(new OutputStreamWriter
                                             (new FileOutputStream(f)));
            for (int i = 0; i < 1000000; i++) {
                ps.print(String.valueOf(i));
            }
            ps.close();
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

Python:

f=open('scratch','wb')
for i in xrange(1000000):
    f.write(str(i))
f.close()