官术网_书友最值得收藏!

Module loading with namespace packages

As mentioned earlier, starting with Python 3.3, it's possible to have a package folder that doesn't contain an init file. Leaving out the init file means we can't support import* or the other tricks within it that we'll discover as we go along. But, there's more to it than that. The following screenshot shows a code example for this:

When the init file is missing, the folder becomes part of a namespace package folder. When Python is importing, it combines all of the namespace package folders it finds, that share a name, into a single logical package, as shown in the following screenshot:

This behavior means that while choosing the module filename, Python still follows the same rules, we could potentially place that file into one of any number of namespace package folders instead of into a singular concrete package folder.

What do we gain from that? Often nothing!

As I mentioned earlier, packages with an init load faster, and in many cases, the extra abstraction of namespace packages doesn't buy us anything.

There are cases, however, when we want different parts of the same package to be distributed or managed separately and when we do, namespace packages address that need. For example, imagine again that we are working on a package for playing soundtracks. If we make a namespace package folder for audio codecs, each codec could be installed and removed individually using pip or the operating system's normal package management tools.

On a slightly different topic, now let's talk about the difference between how a package is structured and the interface it should present for use by external code.

主站蜘蛛池模板: 邻水| 合江县| 德阳市| 玉门市| 曲麻莱县| 城市| 温泉县| 嘉义市| 通海县| 神农架林区| 余庆县| 通海县| 兴山县| 齐齐哈尔市| 保定市| 泸西县| 阿克苏市| 鄯善县| 连江县| 禄丰县| 玉树县| 建平县| 天等县| 神木县| 稷山县| 莱阳市| 汕头市| 乌苏市| 沧州市| 黑龙江省| 富顺县| 柳河县| 上高县| 赣榆县| 白山市| 玛曲县| 洛川县| 云龙县| 于都县| 浮梁县| 桐城市|