编码样式(PEP8)-模块级别"dunders" [英] Coding style (PEP8) - Module level "dunders"

查看:121
本文介绍了编码样式(PEP8)-模块级别"dunders"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"Dunder"的定义( D ouble 分数):

Definition of "Dunder" (Double underscore): http://www.urbandictionary.com/define.php?term=Dunder

我有一个问题要问Python代码中模块级别"dunders"(如__all____version____author__等)的位置.

I have a question according the placement of module level "dunders" (like __all__, __version__, __author__ etc.) in Python code.

在阅读 PEP8 并看到<一个href ="https://stackoverflow.com/questions/24741141/pycharm-have-author-appear-before-imports">此堆栈溢出问题.

The question came up to me while reading through PEP8 and seeing this Stack Overflow question.

可接受的答案是:

__author__是全局变量",因此应出现在导入的下方.

__author__ is a global "variable" and should therefore appear below the imports.

但是在PEP8部分模块级dunder名称我读到以下内容:

But in the PEP8 section Module level dunder names I read the following:

模块级"dunders"(即带有两个前导和两个尾随的名称 下划线),例如__all____author____version__等 放在模块文档字符串之后,但在任何导入语句之前 从__future__导入中除外. Python要求将来导入 必须在模块中出现在文档字符串之前的其他代码之前.

Module level "dunders" (i.e. names with two leading and two trailing underscores) such as __all__ , __author__ , __version__ , etc. should be placed after the module docstring but before any import statements except from __future__ imports. Python mandates that future-imports must appear in the module before any other code except docstrings.

作者还提供了一个代码示例:

The authors also give a code example:

"""This is the example module.

This module does stuff.
"""

from __future__ import barry_as_FLUFL

__all__ = ['a', 'b', 'c']
__version__ = '0.1'
__author__ = 'Cardinal Biggles'

import os
import sys

但是,当我将以上内容放入PyCharm时,我会看到此警告(另请参见屏幕截图):

But when I put the above into PyCharm, I see this warning (also see the screenshot):

PEP8:模块级别导入不在文件顶部

PEP8: module level import not at top of file

问题:用双下划线存储这些变量的正确方法/位置是什么?

推荐答案

PEP 8最近被更新,以将位置放置在导入之前.请参见2016年6月7日提交的修订版cf8e888b9555 :

PEP 8 recently was updated to put the location before the imports. See revision cf8e888b9555, committed on June 7th, 2016:

放松__all__位置.

将所有模块级别的叮under放在同一位置,然后删除 冗余版本的簿记信息.

Put all module level dunders together in the same location, and remove the redundant version bookkeeping information.

关闭#27187. Ian Lee的补丁.

Closes #27187. Patch by Ian Lee.

该文本已第二天进行了进一步更新,以解决from __future__ import ...警告.

The text was further updated the next day to address the from __future__ import ... caveat.

该修补程序链接到问题#27187 ,该引用又引用了

The patch links to issue #27187, which in turn references this pycodestyle issue, where it was discovered PEP 8 was unclear.

在进行此更改之前,由于没有关于模块级dunder全局变量的明确指南,因此PyCharm和其他答案当时是正确的.我不确定PyCharm如何实施他们的PEP 8检查;如果他们使用 pycodestyle项目(事实上的Python样式检查器),那么我相信它将自动修复.否则,也许向他们提交错误以解决此问题.

Before this change, as there was no clear guideline on module-level dunder globals, so PyCharm and the other answer were correct at the time. I'm not sure how PyCharm implements their PEP 8 checks; if they use the pycodestyle project (the defacto Python style checker), then I'm sure it'll be fixed automatically. Otherwise, perhaps file a bug with them to see this fixed.

这篇关于编码样式(PEP8)-模块级别"dunders"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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