Steven D'Aprano wrote:
On Wed, 20 Oct 2010 12:47:02 +0200, Jean-Michel Pichavant wrote:
except ValueError, e:
Use meaningful names, this is so important. 'e' is not meaningful.
'exception' would be slighly better.
While I agree with everything else you had to say, I have to take
exception to this comment [pun intended].
"e" as a short name for a generic exception instance is perfectly
reasonable, like:
i, j, k for an index, or a loop variable
e.g. for i in range(100)
n for some other integer variable
s for a string
x for a float, or an arbitrary sequence object
e.g. [x.spam() for x in some_sequence]
and similar.
The last example is very instructive. What do you gain by racking your
brain for a "more meaningful" name instead of x? The obvious
alternatives, "obj" or "item", are equally generic as "x", they don't add
any further information. And how much information do you need? It's easy
to parody:
[some_sequence_item.spam() for some_sequence_item in some_sequence]
The very shortness of the name is valuable because it reduces the *human*
parsing time in reading, and there is no cost because the conventions are
so familiar. The convention of "for i in ..." says "this is a loop over
an integer" so strongly, that I would argue that "for index in ..." would
actually *delay* comprehension.
Furthermore, the use of a single letter cues the reader that this
variable isn't notable -- there's nothing unusual or unconventional about
it, or it isn't the important part of the algorithm, or that its scope is
severely limited. For instance, consider the classic example of
exchanging two variables in Python:
a, b = b, a
versus:
thing, other_thing = other_thing, thing
The first example puts the emphasis on the *technique*, not the
variables. The second obscures it behind needlessly longer but still
generic names.
You are absolutely right to insist on meaningful variable names. Where
you go wrong is to assume that single letter names can't be meaningful.
You are using meaningless example without any context, for sure you
won't find a proper meaningful name.
Let me quote the paper I linked in the previous post:
list1 = []
for x in theList:
if x[0] == 4:
list1 += x;
return list1
compare it to:
flaggedCells = []
for cell in theBoard:
if cell[STATUS_VALUE] == FLAGGED:
flaggedCells += cell
return flaggedCells
There is another important point: the time where code size matters is over. Now
we have the opportunity to write better structured english and that is
priceless. Why to your opinion Python has the 'not in' operator ?.
Code is written once, but read dozen of times so we should pay attention to how
it reads, not really how long it takes to write. You won't be that happy
everyone wrote their mail in this list like a mobile text message, if u c what
I mean.
JM
--
http://mail.python.org/mailman/listinfo/python-list