python - 在 Python 文件中放置 __all__ 的位置?

标签 python pep8 readability code-readability flake8

我想知道,__all__ 在 Python 文件中的标准位置是什么? ?
我的假设直接位于导入语句的下方。但是,我在任何地方都找不到明确说明/询问的内容。所以,一般来说,应该放在哪里 __all__ ?
它将放在下面的示例文件中的什么位置?

#!/usr/bin/env python3

"""Where to put __all__."""

from time import time

# I think it should go here: __all__ = ["Hello"]

SOME_GLOBAL = 0.0


class Hello:
    def __init__(self):
        pass


if __name__ == "__main__":
    pass
先感谢您!

最佳答案

根据 PEP 8 :

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.



因此,如果您严格遵循 PEP 8,那么您就很接近了。在实践中,并没有严格遵守。大量 Python“包含电池”模块定义 __all__在导入之后,所以你的方法非常好。

关于python - 在 Python 文件中放置 __all__ 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638571/

相关文章:

python - 如何使用 pyqt4 清除带有索引的表小部件中的数据

python - 在列表中映射字典键值

php - 如何使 SQL 查询在 PHP 中更具可读性?

sql - 如何在 VB.NET 中编写可读的 SQL

python - 使用 SymPy 通过给定点构造符号插值样条

python - 是否有可用于 Python 的可变长度哈希函数?

python - Python 的 PEP8 行长度限制是否适用于注释?

python - 在尊重 pep8 的同时访问嵌套数组中的值的最优化方法是什么?

python - 符合 pep8 的深度字典访问

c - 初始化 C 结构体与分配成员