1

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 module builtin to your own custom importer function—for example, one using the generic built-in-wrapping technique shown in “Python built-ins” on page 174.

  1. In "You can rebind the __import__ attribute of the module builtin", should "the module builtin" be "the module builtins" instead?

  2. Is "the __import__ attribute of the module builtin" bound to importlib.__import__function by default? Or does "the module builtin" provide the default implementation bound to its __import__ attribute?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

3
  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.

  2. builtins.__import__ is a distinct function from importlib.__import__. If you are going to rebind builtins.__import__, save a reference.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks. Do you mean that `importlib.__import__` implement the same things as `builtins.__import__`? The only difference between them is in which language they are implemented? – Tim Jun 20 '17 at 14:32
  • @Tim: exactly, the two *aim* to do the same thing. – Martijn Pieters Jun 20 '17 at 14:35