为什么 pylint 在函数外部时需要大写的变量名? [英] Why does pylint require capitalized variable names when outside a function?
问题描述
为什么pylint在函数外部接受大写变量而在函数内部拒绝它们?反之,为什么 pylint 拒绝函数外的camelCase而在函数内部接受它?
Why does pylint accept capitalized variables when outside a function and reject them inside a function? Conversely, why does pylint reject camelCase ouside a function and accept it inside a function?
我刚刚安装了 pylint(版本 2.2.2)来检查我的 Python 3.一定有我遗漏的地方.我的相关 Python/包版本是:
I just installed pylint (version 2.2.2) to check my Python 3. There must be something that I missed. My relevant Python/package versions are:
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),其中我使用驼峰命名法和大写字母命名的变量.接受大写变量(为什么?)并拒绝驼峰式命名(因为代码没有包装到函数中,我猜).
Consider the following code (test_1) where I'm using camelCase and Capitalized named for variables. The Capitalized variable are accepted (why ?) and camelCase rejected (because the code is not wrapped into a function, I guess).
'''
Nothing important
'''
fileHandler = open("afile.txt")
for line in fileHandler:
Token = line.split("\t")
Part_1 = Token[0]
print(Part_1)
调用 pylint 时给出:
Which give upon calling 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) 中.
Now if I put everything into a function (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()
然后大写变量被检测为不合规(这是我所期望的):
Then the capitalized variable are detected as non compliant (which is what I expected) :
$ 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)
我有不清楚的地方...欢迎澄清...
There is something unclear for me... Any clarification welcome...
最佳
推荐答案
对于在 for 循环中接受的大写变量(即以大写字母开头,例如 Token, Part_1)的情况感到困惑(请参阅 test_1)虽然他们应该被拒绝.我在github上发布了一个问题.这个问题揭示了 pylint 中的一个错误.https://github.com/PyCQA/pylint/issues/2695
I was puzzled regarding the case of capitalized variables (i.e starting with an uppercase letter, e.g. Token, Part_1) that were accepted in the for loops (see test_1) while they should have been rejected. I have posted an issue in github. This issue revealed a bug in pylint. https://github.com/PyCQA/pylint/issues/2695
这篇关于为什么 pylint 在函数外部时需要大写的变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!