问题
From Python in a Nutshell
Custom Importers
An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements.
Rebinding __import__
You can rebind the
__import__attribute of the modulebuiltinto your own custom importer function—for example, one using the generic built-in-wrapping technique shown in “Python built-ins” on page 174.
In "You can rebind the
__import__attribute of the modulebuiltin", should "the modulebuiltin" be "the modulebuiltins" instead?Is "the
__import__attribute of the modulebuiltin" bound to importlib.__import__function by default? Or does "the modulebuiltin" provide the default implementation bound to its__import__attribute?
回答1:
Yes, that's a typo in the book. In Python 2 the same module is named __builtin__ (no
s), in Python 3 it is named builtins.builtins.__import__is a distinct function fromimportlib.__import__. If you are going to rebindbuiltins.__import__, save a reference.builtins.__import__is implemented in C, and essentially calls the C-API PyImport_ImportModuleLevelObject function.importlib.__import__is a pure-Python function. The goal ofimportlibis to provide a pure-python implementation of the import machinery so it can be hacked on more easily, and this function is no exception.
来源:https://stackoverflow.com/questions/44655446/what-is-the-default-binding-to-the-import-attribute-of-the-module-builtin