为什么pylint在函数外部时接受大写变量,而在函数内部拒绝它们?相反,为什么pylint拒绝camelCase ousIDe函数并在函数内部接受它?
我刚刚安装了pylint(版本2.2.2)来检查我的Python3.一定错过了某些东西.我相关的Python /软件包版本是:
pylint 2.2.2astroID 2.1.0Python 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和大写的变量命名.大写的变量被接受(为什么?)而骆驼的情况被拒绝(我想是因为代码没有包装到函数中).
'''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_1test_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_2test_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)
我有一些不清楚的地方…欢迎澄清…
最好
最佳答案当您将变量放在函数内部时,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 python3def 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所述). 总结
以上是内存溢出为你收集整理的为什么在函数外时pylint需要大写的变量名? 全部内容,希望文章能够帮你解决为什么在函数外时pylint需要大写的变量名? 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)