- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我只想知道__package__
到底是什么意思?在官方文档中没有找到任何解释,即使在 SO 上也是如此。
如果你能提供一些例子,我会很高兴。
最佳答案
见 PEP 366和 import 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 toNone
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/
如果在我的代码中,我会调用如下: __PACKAGE__->method; 如果这个模块是继承的,这会限制这个模块的可用性吗? 最佳答案 这取决于你想做什么: #!/usr/bin/perl pack
我正在重构遗留代码中的 perl 模块,这是模块中的一个函数: sub get_user { my $user = __PACKAGE__->{user}; if (!defined
我在 setup.py 中使用 __package__ 来引用它应该测试、构建、安装等的包的顶级名称。但是,pylint 对象: Module 'mian.mian' has no '__packag
我只想知道__package__ 到底是什么意思?在官方文档中没有找到任何解释,即使在 SO 上也是如此。 如果你能提供一些例子,我会很高兴。 最佳答案 见 PEP 366和 import syste
在这个 earlier Stackoverflow question尤其是 brian d foy's "How a Script Becomes a Module"我已经阅读了如何设置代码,以便它可
我希望我知道告诉我这是在 Perl 中创建新对象的最佳方法的引用或文档: sub new { my $package = shift; my $class = ref($package)
我想通过以下方式动态导入模块: 我创建了一个名为 pkg 的文件夹,结构如下: pkg |__init__.py |foo.py 在__init__.py的头部,添加如下代码片段: pkg
我读了很多帖子,与这个问题相关的PEP文章,大约有4篇,但是没有一篇在某些点上给出了清晰的想法,我仍然无法进行相对导入。 事实上,我的主包中的内容根本没有列出。 重新编辑。我把帖子全部修改了,太复杂了
我在一个类中有一个方法,我需要确保它只在对象实例上调用,而不是作为类方法。 我可能会做这样的事情: # Edit: this is terrible, don't do this, it breaks
我是一名优秀的程序员,十分优秀!