错误信息“python-pylint 'C0103:Invalid constant name" [英] Error message "python-pylint 'C0103:Invalid constant name"

查看:31
本文介绍了错误信息“python-pylint 'C0103:Invalid constant name"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这张照片中的错误感到困惑:

I'm confused about the error(s) in this photo:

我不知道如何修复它们.我的程序是一个 Python-Flask 网页框架.当我使用 Visual Studio Code 调试我的程序时,Pylint 会显示这些错误.我知道这个问题并不重要,但它让我很恼火.我该如何解决?

I don't know how to fix them. My program is a Python-Flask web frame. When I use Visual Studio Code 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?

# -*- 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()

推荐答案

正如 Kundor 所解释的那样,PEP 8 指出:

As explained by Kundor, PEP 8 states that:

常量通常在模块级别定义,并以所有大写字母书写,并用下划线分隔单词.

Constants are usually defined on a module level and written in all capital letters with underscores separating words.

重点是常量"在 Python 中并不真正存在.根据 PEP 8,Pylint 期望模块级变量为常量".

The point is that "constants" in Python don't really exist. Pylint, as per PEP 8, expects module level variables to be "constants."

话虽如此,您有多种选择:

That being said you've several options:

  • 你不想要这个常数"事情,然后将 Pylint 的 const-rgx 正则表达式更改为与例如相同变量-rgx,

  • you don't want this "constant" thing, then change Pylint's const-rgx regular expression to be the same as e.g. variable-rgx,

你可以使用 # pylint: disable=invalid-name,

避免使用模块级变量,方法是将它们包装到一个函数中.

avoid module level variables, by wrapping them into a function.

在你的情况下,我会选择第三个选项,通过创建一个 build_app 函数或类似的东西.这将返回应用程序(也可能返回 'db' 对象,但您有多种选择).然后你可以添加第二个选项的盐来得到类似的东西:

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:

app = build_app() # pylint: disable=invalid-name

这篇关于错误信息“python-pylint 'C0103:Invalid constant name"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆