gpt4 book ai didi

python - Python 中 "__package__"属性的用途是什么?

转载 作者:IT老高 更新时间:2023-10-28 22:02:35 28 4
gpt4 key购买 nike

我只想知道__package__ 到底是什么意思?在官方文档中没有找到任何解释,即使在 SO 上也是如此。

如果你能提供一些例子,我会很高兴。

最佳答案

PEP 366import system reference documentation :

The major proposed change is the introduction of a new module level attribute, __package__. When it is present, relative imports will be based on this attribute rather than the module __name__ attribute.

  • The module’s __package__ attribute should be set. Its value must be a string, but it can be the same value as its __name__. If the attribute is set to None or is missing, the import system will fill it in with a more appropriate value. When the module is a package, its __package__ value should be set to its __name__. When the module is not a package, __package__ should be set to the empty string for top-level modules, or for submodules, to the parent package’s name. See PEP 366 for further details.

因此,对于位于 foo/bar/baz.py 中的模块,__name__ 设置为 foo.bar.baz,并且__package__ 设置为 foo.bar,而 foo/bar/__init__.py 将具有 foo.bar __name____package__ 属性。

关于python - Python 中 "__package__"属性的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21233229/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com