【问题标题】:Why does pylint require capitalized variable names when outside a function?为什么 pylint 在函数之外需要大写的变量名?
【发布时间】:2019-06-06 15:38:33
【问题描述】:

为什么 pylint 在函数外部接受大写变量而在函数内部拒绝它们?相反,为什么 pylint 在函数外拒绝 camelCase 并在函数内接受它?

我刚刚安装了 pylint(版本 2.2.2)来检查我的 Python 3。一定有什么我错过了。我的相关 Python/包版本是:

pylint 2.2.2
astroid 2.1.0
Python 3.6.7 | packaged by conda-forge | (default, Nov 20 2018, 18:20:05)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]

考虑以下代码 (test_1),其中我使用 camelCase 和 Capitalized 命名变量。 Capitalized 变量被接受(为什么?)并且 camelCase 被拒绝(因为代码没有被包装到函数中,我猜)。

'''
Nothing important
'''

fileHandler = open("afile.txt")

for line in fileHandler:
    Token = line.split("\t")
    Part_1 = Token[0]
    print(Part_1)

在调用 pylint 时给出:

$ pylint --py3k --enable=all  test_1.py 
************* Module test_1
test_1.py:5:0: C0103: Constant name "fileHandler" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)

现在,如果我将所有内容都放入一个函数 (test_2)。

'''
Nothing important
'''

def foo():
    fileHandler = open("afile.txt")

    for line in fileHandler:
        Token = line.split("\t")
        Part_1 = Token[0]
        print(Part_1)

if __name__ == '__main__':
    foo()

然后大写的变量被检测为不合规(这是我的预期):

$ pylint --py3k --enable=all  test_2.py
************* Module test_2
test_2.py:5:0: C0102: Black listed name "foo" (blacklisted-name)
test_2.py:5:0: C0111: Missing function docstring (missing-docstring)
test_2.py:6:4: C0103: Variable name "fileHandler" doesn't conform to snake_case naming style (invalid-name)
test_2.py:9:8: C0103: Variable name "Token" doesn't conform to snake_case naming style (invalid-name)
test_2.py:10:8: C0103: Variable name "Part_1" doesn't conform to snake_case naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 3.75/10 (previous run: 3.75/10, +0.00)

我有一些不清楚的地方...欢迎任何澄清...

最好的

【问题讨论】:

    标签: python pylint capitalize


    【解决方案1】:

    我对大写变量(即以大写字母开头,例如 Token,Part_1)在 for 循环(参见 test_1)中被接受而本应被拒绝的情况感到困惑。我在 github 上发布了一个问题。这个问题揭示了 pylint 中的一个错误。 https://github.com/PyCQA/pylint/issues/2695

    【讨论】:

      【解决方案2】:

      当您将变量放入函数中时,pylint 不再将它们“视为”常量。将变量放入函数后,pylint 将它们“视为”普通变量,不再要求您将它们大写,而是需要“snake_case”。注意,pylint 默认更喜欢snake_case 而不是camelCase,但您可以在.pylintrc 中使用override this 来更喜欢camelCase。

      Python 脚本(无方法)

      #!/usr/bin/env python3
      
      # pylint wants 'FILEHANDLER'
      fileHandler = open("afile.txt") # <-- pylint sees constant, wants UPPER_CASE 
      
      for line in fileHandler:
          Token = line.split("\t")
          Part_1 = Token[0]
          print(Part_1)
      

      使用方法

      #!/usr/bin/env python3
      
      def run_stuff():
      
          # pylint wants 'file_handler'
          fileHandler = open("afile.txt") # <-- pylint sees normal variable
      
          for line in fileHandler:
              Token = line.split("\t")
              Part_1 = Token[0]
              print(Part_1)
      
      if __name__ == '__main__':
          run_stuff()
      

      通常,.pylintrc 文件将跟随PEP8。如果没有提供,它将默认为 PEP8,如 pylint website 中所述。愉快的 linting!

      【讨论】:

      • 非常感谢 Scott 的回复。我很好理解 fileHandler 的情况,它在 test_1 中被视为常量,应该写成大写。我特别担心 test_1 中的 'Token and Part_1' 应该写成 'token' 和 'part_1' 吗? PEP8 表示变量名应该是小写的。所以两者都应该被检测为不合规?没有?
      • token 和 part_1 应该全部小写,不能以任何大写开头。
      • 是的,它们应该是小写的。那么为什么他们在 test_1 中被 pylint 接受?
      • 你在问什么?我回答你的问题了吗?
      • 对不起,如果我的问题不清楚,因为它混合了与大写 (FOO) 变量相关的问题,并以大写 (Foo) 变量开头。请在 github 中查看我的答案和问题。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2020-08-20
      相关资源
      最近更新 更多