gpt4 book ai didi

python - 如何告诉pylint类实例变量不是常量

转载 作者:行者123 更新时间:2023-11-28 17:43:16 24 4
gpt4 key购买 nike

class MyFavoriteClass():
def __init__(self):
self.counter = 0
def memberFunction(self):
self.counter = self.counter + 1

myinstance = MyFavoriteClass() #Pylint complains here
myinstance.memberFunction()

倒数第二行pylint报错是

[C0103] Invalid name "myinstance" for type constant (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)

我读到过可以完全禁用此类错误,但是有必要吗?

我如何告诉 pylint 我的实例不是常量?

pylint -v 报告的系统配置

pylint 0.26.0, 
astng 0.24.1, common 0.59.1
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
[GCC 4.8.1]

最佳答案

使用 pylint,您可以通过使用以下格式的特殊注释来忽略一行中的个别错误、警告等:# pylint: disable={comma-separated-list- of-names-or-codes

class MyFavoriteClass():
def __init__(self):
self.counter = 0
def memberFunction(self):
self.counter = self.counter + 1

myinstance = MyFavoriteClass() # pylint: disable=invalid-name
myinstance.memberFunction()

或者,您可以在 pylint 配置文件中指定要忽略的消息代码列表:

[MESSAGES CONTROL]

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
disable=invalid-name
# OR
disable=C0103

注意:禁用配置文件中的代码将全局忽略它。如果您只想在特定行上忽略它,则必须使用开头提到的特殊注释。

pylint 配置文件是通过首先检查 PYLINTRC 环境变量来确定的。如果没有提供文件,则连续检查 ~/.pylintrc/etc/pylintrc

如果您可以控制正在执行的pylint 命令,您还可以使用--rcfile 参数指定配置。

如果您想生成示例配置,请运行:

pylint --generate-rcfile

此外,如果您禁用代码,将触发 locally-disabled (I0011)它本身也可以禁用(最好在配置中)。

关于python - 如何告诉pylint类实例变量不是常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com