- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 277字
- 2021-07-02 21:09:38
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.
- 手機安全和可信應(yīng)用開發(fā)指南:TrustZone與OP-TEE技術(shù)詳解
- Getting Started with Citrix XenApp? 7.6
- Cocos2D-X權(quán)威指南(第2版)
- Mastering Ember.js
- Ext JS Data-driven Application Design
- MySQL 8 DBA基礎(chǔ)教程
- 數(shù)據(jù)結(jié)構(gòu)(C語言)
- SharePoint Development with the SharePoint Framework
- Learning Apache Karaf
- Mobile Device Exploitation Cookbook
- Python函數(shù)式編程(第2版)
- 嵌入式Linux C語言程序設(shè)計基礎(chǔ)教程
- 計算語言學導論
- AI自動化測試:技術(shù)原理、平臺搭建與工程實踐
- Mastering ASP.NET Core 2.0