[Python-projects] Parameters with leading "_" shouldn't count as "local" variables
skip at pobox.com
skip at pobox.com
Tue May 27 21:19:56 CEST 2008
Frequently, an external callback mechanism will dictate a fixed set of
parameters, some of which some callback functions won't need. To avoid
pylint warnings about unused variables, the usual technique used is to
prefix such args with an underscore, e.g.:
def callback(_a, _b, c, _d):
print c
If your callback function is fairly complex it might well use several local
variables to hold intermediate computations. In this stupid example, pylint
complains (among other things) about having too many local variables:
def cb(a, b, c, d, e, f, _g, _h, _i, _j):
k = a ** 2
l = b ** 3
m = c ** 4
n = d ** 5
o = e ** 6
p = f ** 7
If _g, _h, _i and _j are eliminated from consideration as "local variables"
this function only has 12 locals, not 16. From a cognitive standpoint, by
using the "_" prefix that is exactly what I have declared. "These are not
important to me. I am ignoring them and you, the reader (or pylint), should
ignore them as well."
I can do this:
def callback(_a, _b, c, _d):
return _callback(c)
def _callback(c):
print c
but that seems inelegant, to say the least. In addition, if some of those
local variables are used to avoid repeating the same complex or expensive
calculation, adding an extra (expensive) function call to work around pylint
seems just plain wrong.
Can you change pylint to avoid counting explicitly unused parameters against
me? FYI, here's my environment:
% pylint --version
pylint 0.14.0,
astng 0.17.2, common 0.28.2
Python 2.4.5 (#4, Apr 12 2008, 09:09:16)
[GCC 3.4.1]
Thanks,
--
Skip Montanaro - skip at pobox.com - http://www.webfast.com/~skip/
"Be different, express yourself like everyone else."
More information about the Python-Projects
mailing list