[Python-projects] Parameters with leading "_" shouldn't count as "local" variables

Sylvain Thénault sylvain.thenault at logilab.fr
Tue Jul 8 10:44:44 CEST 2008


Hi Skip,

sorry for the late answer, I've been busy and profit now of my return 
from vacation to do stuff I've delayed again and again...

On Tue, May 27, 2008 at 02:19:56PM -0500, skip at pobox.com wrote:
> 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?  
I've created a ticket for your feature request:
http://www.logilab.org/ticket/5564

-- 
Sylvain Thénault                               LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian:  http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
Python et calcul scientifique:           http://www.logilab.fr/science



More information about the Python-Projects mailing list