diff options
author | Brett Cannon <brett@python.org> | 2013-06-14 19:04:26 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-06-14 19:04:26 (GMT) |
commit | 3fe35e65034de82c45e2d8fe1ebe4a2929c68453 (patch) | |
tree | 5417c105714337f251daf3eb8be5f6762d273d99 /Doc/library | |
parent | 6f1057605b26c34b30c562a1b620984ed1211f39 (diff) | |
download | cpython-3fe35e65034de82c45e2d8fe1ebe4a2929c68453.zip cpython-3fe35e65034de82c45e2d8fe1ebe4a2929c68453.tar.gz cpython-3fe35e65034de82c45e2d8fe1ebe4a2929c68453.tar.bz2 |
Issue #18193: Add importlib.reload(), documenting (but not
implementing in code) the deprecation of imp.reload().
Thanks to Berker Peksag for the patch.
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/imp.rst | 3 | ||||
-rw-r--r-- | Doc/library/importlib.rst | 67 |
2 files changed, 70 insertions, 0 deletions
diff --git a/Doc/library/imp.rst b/Doc/library/imp.rst index e090e00..8a75d4c 100644 --- a/Doc/library/imp.rst +++ b/Doc/library/imp.rst @@ -171,6 +171,9 @@ This module provides an interface to the mechanisms used to implement the the class does not affect the method definitions of the instances --- they continue to use the old class definition. The same is true for derived classes. + .. deprecated:: 3.4 + Use :func:`importlib.reload` instead. + The following functions are conveniences for handling :pep:`3147` byte-compiled file paths. diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst index d9e4273..0caabaa 100644 --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -115,6 +115,73 @@ Functions .. versionadded:: 3.3 +.. function:: reload(module) + + Reload a previously imported *module*. The argument must be a module object, + so it must have been successfully imported before. This is useful if you + have edited the module source file using an external editor and want to try + out the new version without leaving the Python interpreter. The return value + is the module object (the same as the *module* argument). + + When :func:`.reload` is executed: + + * Python modules' code is recompiled and the module-level code re-executed, + defining a new set of objects which are bound to names in the module's + dictionary by reusing the :term:`loader` which originally loaded the + module. The ``init`` function of extension modules is not called a second + time. + + * As with all other objects in Python the old objects are only reclaimed + after their reference counts drop to zero. + + * The names in the module namespace are updated to point to any new or + changed objects. + + * Other references to the old objects (such as names external to the module) are + not rebound to refer to the new objects and must be updated in each namespace + where they occur if that is desired. + + There are a number of other caveats: + + If a module is syntactically correct but its initialization fails, the first + :keyword:`import` statement for it does not bind its name locally, but does + store a (partially initialized) module object in ``sys.modules``. To reload + the module you must first :keyword:`import` it again (this will bind the name + to the partially initialized module object) before you can :func:`reload` it. + + When a module is reloaded, its dictionary (containing the module's global + variables) is retained. Redefinitions of names will override the old + definitions, so this is generally not a problem. If the new version of a + module does not define a name that was defined by the old version, the old + definition remains. This feature can be used to the module's advantage if it + maintains a global table or cache of objects --- with a :keyword:`try` + statement it can test for the table's presence and skip its initialization if + desired:: + + try: + cache + except NameError: + cache = {} + + It is legal though generally not very useful to reload built-in or + dynamically loaded modules (this is not true for e.g. :mod:`sys`, + :mod:`__main__`, :mod:`__builtin__` and other key modules where reloading is + frowned upon). In many cases, however, extension modules are not designed to + be initialized more than once, and may fail in arbitrary ways when reloaded. + + If a module imports objects from another module using :keyword:`from` ... + :keyword:`import` ..., calling :func:`reload` for the other module does not + redefine the objects imported from it --- one way around this is to + re-execute the :keyword:`from` statement, another is to use :keyword:`import` + and qualified names (*module.name*) instead. + + If a module instantiates instances of a class, reloading the module that + defines the class does not affect the method definitions of the instances --- + they continue to use the old class definition. The same is true for derived + classes. + + .. versionadded:: 3.4 + :mod:`importlib.abc` -- Abstract base classes related to import --------------------------------------------------------------- |