why python is slower than java?
Alex Martelli
aleaxit at yahoo.com
Sun Nov 7 13:36:09 EST 2004
Roy Smith <roy at panix.com> wrote:
> 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.
I agree, and efficiency is just the same if you _do_ give names to
intermediate objects. Terseness is worthy as long as it _helps_ reading
the code, by avoiding violations of Occam's Razor (introducing entities
without necessity).
> 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.
It's actually right-to-left, as that's the way the inside-out order
works out. But if you read left->right, it's "to file
sorted_autoexec.bak, write lines from sorted file autoexec.bat", which
doesn't scan all THAT badly in English, I think;-). It's right-left if
you think of things HAPPENING, of IMPERATIVE code, but it's not
necessarily that way if you think of the code as descriptive;-).
> You could refactor this in a multitude of ways, with varying levels of
> compactness or verbosity, depending on how many intermediate variables
Right.
> 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".
My preference here might be to have a name for the output file variable
and not for the input one, but that's a pretty thin consideration. Your
version lets one close both files explicitly, which is good.
> > 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 :-)
Heh -- you're a notch up from me, since the wireless Mac laptop I'm
using is a humble iBook 12";-).
Alex
More information about the Python-list
mailing list