pylint #5564 Parameters with leading "_" shouldn't count as "local" variables [resolved]
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.
syt note: we should add a regexp to match variables we don't want to count.
|closed by||<not specified>|
- pylint #5977 yield != return
- pylint #2467 similarities in the same file are not considered
- pylint #2471 branch count of a function include subfunctions
- pylint #4983 new py2.5 check: test "yield" in "try" body with yield in exception handlers.
- pylint #9776 warning if return or break inside a finally