pylint -- should I just ignore it sometimes?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Oct 20 21:37:21 CEST 2010


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.



-- 
Steven



More information about the Python-list mailing list