我想知道,__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/