Python中的模块名能否以下划线结尾?

2024-06-28 11:05:20 发布

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

下面是我面临的问题的一个例子:


项目

  • 包装:
    • xml.py

xml.py

import xml.etree.ElementTree as Et


我的名为xml.py的模块在导入时与xml包混淆,现在我的问题是:

我可以用这样一种方式命名模块吗?这种方式不会干扰包名,但同时符合PEP8样式指南(例如:xml_.py。我见过变量的这种命名约定,但我不确定这样命名模块是否是一种好的做法)


Tags: 模块项目pyimportas方式指南样式
2条回答

关于模块名称的PEP-8指南规定:

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

此外,另一个国家指出:

single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.:

。。。这正是你这么做的原因,而不仅仅是为了好玩。 因此,即使它没有明确地作为模块的命名准则,我假设我们可以将其适应所有Python命名约定。所以我认为你可以走了

模块(文件名)应该有短的、全小写的名称,并且可以包含下划线

Pep8文档,所以您可以:)

相关问题 更多 >