why python is slower than java?

Roy Smith roy at panix.com
Sun Nov 7 13:10:38 EST 2004


aleaxit at yahoo.com (Alex Martelli) wrote:

> file('sorted_autoexec.bak','w').writelines(sorted(file('autoexec.bat')))
> 
> 73 chars in one line.  Yeah, I cheated -- I _would_ normally put a space
> after the comma, making the real total into seventyFOUR...

I understand what you're doing with the above one-liner, but I don't 
think that's the best way to write it.  Unless there's some over-riding 
efficiency requirement, I'm always going to vote for easier to read over 
fewer characters.

Chaining all those operations together into one big line makes it (IMHO) 
more difficult to understand what's going on, especially since it's a 
mix of method calls and global function calls.  To understand the 
sequence of operations, you need to read from the inside-out starting 
from two different places, then put those two conceptual units together 
from left-to-right.

You could refactor this in a multitude of ways, with varying levels of 
compactness or verbosity, depending on how many intermediate variables 
you want to use.  I think I would go for something like:

inFile = file ('autoexec.bat')
outFile = file ('sorted_autoexec.bak', 'w')
outFile.writelines (sorted (inFile))

Some would say that the intermediate variables just add bulk, but I 
think they provide conceptual punctuation, i.e. a place for the reader 
to say, "OK, I understand that chunk, now let's see what the next chunk 
does".

> one can almost see you writing it with a quill dipped in
> ink, at a carved oak desk, on your steam-powered computer.  Charming!
> 
> Should you ever decide to move into the 21st century, though, don't
> worry: Python will be there to help you do so.

I know you didn't write those lines with me in mind, but I can't help 
chuckle over them.  I'm tagged as a luddite by my co-workers because I 
still don't have a cell phone or a palm pilot or an MP-3 player, not to 
mention that I know what the program drum is used for on an 029 card 
punch.  I am however typing this from my wireless PowerBook :-)



More information about the Python-list mailing list