Pylint showing invalid variable name in output

前端 未结 3 1447
北荒
北荒 2020-12-23 13:22

I made a simple python script to post data on a website.

#Imports

url_to_short = sys.argv[1]

post_url = \'https://www.googleapis.com/urlshortener/v1/url\'
         


        
相关标签:
3条回答
  • 2020-12-23 13:54

    As your code is not contained in a class or function it is expecting those variables to be constants and as such they should be uppercase.

    You can read PEP8 for further information.

    0 讨论(0)
  • 2020-12-23 14:01

    It's because url_to_short is declared in the global namespace, and pylint requires global variables (e.g. constants) to be named ALL_UPPERCASE.
    Therefore it checks whether your variable name matches the regex used for globals, which is: (([A-Z_][A-Z0-9_]*)|(__.*__))$ (note the A-Z ranges). Hence the Invalid name error.

    0 讨论(0)
  • 2020-12-23 14:12

    EDIT: As others have mentioned, pylint expects that global variables should be UPPERCASE. If the warnings really bother you, you can circumvent them by wrapping small snippets like this in a main()-function and then use the if __name__ == "__main__"-convention. Or if you care, you can modify the regular expressions that pylint uses to validate variable names.

    From the developers of Pylint.

    In this case Pylint is telling me that those variables appear to be constants and should be all UPPERCASE. This rule is in fact a naming convention that is specific to the folks at Logilab who created Pylint. That is the way they have chosen to name those variables. You too can create your own in-house naming conventions but for the purpose of this tutorial, we want to stick to the PEP-8 standard. In this case, the variables I declared should follow the convention of all lowercase. The appropriate rule would be something like: "should match [a-z_][a-z0-9_]{2,30}$". Notice the lowercase letters in the regular expression (a-z versus A-Z)

    You can test it by running: pylint --const-rgx='[a-z_][a-z0-9_]{2,30}$' x.py

    0 讨论(0)
提交回复
热议问题