python-pylint 'C0103: 常量名无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41204932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python-pylint 'C0103:Invalid constant name
提问by Xing
I'm confused about the error(s) in this photo:
我对这张照片中的错误感到困惑:
I don't know how to fix them. My program is a Python-Flask web frame. When I use VScode to debug my program, Pylint shows these errors. I know this problem doesn't matter, but it makes me annoyed. How Can I fix it?
我不知道如何修复它们。我的程序是一个 Python-Flask 网络框架。当我使用 VScode 调试我的程序时,Pylint 会显示这些错误。我知道这个问题并不重要,但它让我很恼火。我该如何解决?
# -*- coding: utf-8 -*-
import sys
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_moment import Moment
#from flask_wtf import Form
#from wtforms import StringField, SubmitField
#from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy
reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/test?'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)
if __name__ == '__main__':
db.create_all()
app.run()
回答by sthenault
As explained by Kundor, PEP 8states that:
正如 Kundor 所解释的那样,PEP 8指出:
Constants are usually defined on a module level and written in all capital letters with underscores separating words.
常量通常在模块级别定义,并以所有大写字母书写,下划线分隔单词。
The point is that "constants" in python don't really exist. Pylint, as per PEP 8, expects module level variables to be "constants."
关键是python中的“常量”实际上并不存在。根据 PEP 8,Pylint 期望模块级变量是“常量”。
That being said you've several options:
话虽如此,您有几种选择:
you don't want this "constant" thing, then change pylint's
const-rgx
regular expression to be the same as e.g.variable-rgx
,you may deactivate those warnings for this file, or even locally in the file, using
# pylint: disable=invalid-name
,avoid module level variables, by wrapping them into a function.
你不想要这个“常量”的东西,然后将 pylint 的
const-rgx
正则表达式更改为与 eg 相同variable-rgx
,您可以停用此文件的这些警告,甚至可以在文件中使用
# pylint: disable=invalid-name
,通过将它们包装到函数中来避免模块级变量。
In your case I would go with the third option, by creating a build_app
function or something similar, that would return the application (and maybe the 'db' object as well but you have several choices there). Then you could add a salt of the second option to get something like:
在你的情况下,我会选择第三个选项,通过创建一个build_app
函数或类似的东西,这将返回应用程序(也许还有 'db' 对象,但你有几个选择)。然后你可以添加第二个选项的盐来得到类似的东西:
app = build_app() # pylint: disable=invalid-name
app = build_app() # pylint: disable=invalid-name
回答by T.M.
The fact that PEP8 considers only constants at the module level is probably the reason why many developpers use a dedicated main()function.
PEP8 仅考虑模块级别的常量这一事实可能是许多开发人员使用专用main()函数的原因。
So you could solve your problem like this:
所以你可以像这样解决你的问题:
def main():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost:3306/test?'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)
db.create_all()
app.run()
if __name__ == '__main__':
main()
回答by Nick Matteo
PEP 8decrees that names of constants should be in all caps.
So, rename those variables to be all caps.
因此,将这些变量重命名为全部大写。
Tip: if you google 'C0103' it'll take you to the PyLint messages wiki entry for that message, with details on it.
提示:如果您在 google 上搜索“C0103”,它会带您到该消息的 PyLint 消息 wiki条目,其中包含详细信息。
回答by JChen___
Rename those variables to be all caps.
将这些变量重命名为全部大写。
Such as
如
app = Flask(__name__) => APP = Flask(__name__)
bootstrap = Bootstrap(app) => BOOTSTRAP = Bootstrap(app)
回答by Ankita singh
you can use the following line at the beginning of your python script and execute it:
您可以在 python 脚本的开头使用以下行并执行它:
# pylint: disable=invalid-name
# pylint:禁用=无效名称
It will disable all the invalid constant convention message in static code analyzer.
它将禁用静态代码分析器中的所有无效常量约定消息。