简体   繁体   English

我应该默认创建新的 python 包作为命名空间包吗?

[英]Should I create new python packages by default as namespace packages?

Python 3.3 has implicit namespace packages (PEP420) and in Python 2.7 packages can be marked as namespace packages through adding two lines into __init__py. Python 3.3 具有隐式命名空间包 (PEP420),在 Python 2.7 中,可以通过在 __init__py 中添加两行来将包标记为命名空间包。 See How do I create a namespace package in python .请参阅如何在 python 中创建命名空间包

Given the features the packages enable, it looks like that new packages should be created as namespace packages by default.鉴于包启用的功能,看起来新包应该默认创建为命名空间包。

Normal python packages should only be used in exceptional cases where the need for a "closed" package is obvious.普通的 python 包应该只在明显需要“封闭”包的特殊情况下使用。

What would be drawback of this approach ?这种方法的缺点是什么?

PEP 420 notes regular packages have a performance advantage: PEP 420指出常规包具有性能优势:

There is no intention to remove support of regular packages.无意删除对常规软件包的支持。 If a developer knows that her package will never be a portion of a namespace package, then there is a performance advantage to it being a regular package (with an __init__.py ).如果开发人员知道她的包永远不会成为命名空间包的一部分,那么作为常规包(带有__init__.py )具有性能优势。 Creation and loading of a regular package can take place immediately when it is located along the path.常规包的创建和加载可以在它沿路径定位时立即发生。 With namespace packages, all entries in the path must be scanned before the package is created.对于命名空间包,必须在创建包之前扫描路径中的所有条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM