简体   繁体   中英

How to handle the pylint message: ID:W0612 Unused Variable

I'm updating some code to PEP 8 standard using pylint. Part of the code is throwing the W0612 unused variable error but it's because it's using a module that returns (x,y) for example when only x is needed in this particular case, this is what's done.

(var_1, var_2) = func()

def func():
    a="a"
    b="b"
    return (a,b)

var_1 is then returned but var_2 is never used and therefore throws the error. How should I handle this? I'm thinking this

var = func()[0]

What is the best way to handle it?

I believe that a, dummy = func() does the trick. Pylint allows (if I recall correctly) unused variables names that start with _ or dummy , eg dummy_index .

You can configure this by passing --dummy-variables-rgx option to Pylint. This specifies the regex that catches dummy variable names.

From Pylint 1.6.0 documentation :

dummy-variables-rgx:

A regular expression matching the name of dummy variables (ie expectedly not used). Default: (_+[a-zA-Z0-9]*?$)|dummy

Note : Using _ can indeed cause confusion (props: Sven Marnach). There's a convention to use single underscore as prefix for semi-private identifiers, the double underscore is of course the prefix for special Python methods and on top of that there's a convention to alias gettext() function as _() in programs that need localization as in _("text to translate") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM