如何更正空的初始化文件?

2024-05-14 21:50:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我的包中有几个空的__init__.py文件。如果我保持它们为空,或者我必须在它们内部放置一个pass,这是正确的吗?

有没有关于这个主题的政治公众人物或其他指导方针?


Tags: 文件py主题initpass政治公众人物
1条回答
网友
1楼 · 发布于 2024-05-14 21:50:00

空文件是perfectly fine

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

根据您的计划,这是一个从包中的模块导入公共内容的好地方,这样人们就可以简单地使用from yourpackage import whatever,而不必使用from yourpackage.somemodule import whatever

相关问题 更多 >

    热门问题