Pylint在输出中显示无效的变量名称

发布于 2021-01-29 15:09:09

我做了一个简单的python脚本,将数据发布到网站上。

#Imports

url_to_short = sys.argv[1]

post_url = 'https://www.googleapis.com/urlshortener/v1/url'
headers = {'Content-Type': 'application/json'}

data = {'longUrl': url_to_short}
post_data = json.dumps(data)

req = urllib2.Request(post_url, post_data, headers)
resp = urllib2.urlopen(req)

if resp.getcode() == 200:  
    content = json.loads(resp.read())

#Other stuff

现在我想让我们用pylint工具检查我的脚本中的编码标准。

我的pylint输出如下:

************* Module post
C:  1,0: Missing docstring
C:  6,0: Invalid name "url_to_short" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C:  8,0: Invalid name "post_url" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C:  9,0: Invalid name "headers" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)

# Other stuff

现在,我的问题是为什么pylint将变量名显示为Invalid name。以这种方式命名变量是错误的编码约定。

完整的pylint输出

关注者
0
被浏览
141
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    由于您的代码未包含在类或函数中,因此期望这些变量为常量,因此它们应为大写。

    您可以阅读PEP8以获取更多信息。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看