@@ -115,6 +115,73 @@ Functions
115115
116116 .. versionadded :: 3.3
117117
118+ .. function :: reload(module)
119+
120+ Reload a previously imported *module *. The argument must be a module object,
121+ so it must have been successfully imported before. This is useful if you
122+ have edited the module source file using an external editor and want to try
123+ out the new version without leaving the Python interpreter. The return value
124+ is the module object (the same as the *module * argument).
125+
126+ When :func: `.reload ` is executed:
127+
128+ * Python modules' code is recompiled and the module-level code re-executed,
129+ defining a new set of objects which are bound to names in the module's
130+ dictionary by reusing the :term: `loader ` which originally loaded the
131+ module. The ``init `` function of extension modules is not called a second
132+ time.
133+
134+ * As with all other objects in Python the old objects are only reclaimed
135+ after their reference counts drop to zero.
136+
137+ * The names in the module namespace are updated to point to any new or
138+ changed objects.
139+
140+ * Other references to the old objects (such as names external to the module) are
141+ not rebound to refer to the new objects and must be updated in each namespace
142+ where they occur if that is desired.
143+
144+ There are a number of other caveats:
145+
146+ If a module is syntactically correct but its initialization fails, the first
147+ :keyword: `import ` statement for it does not bind its name locally, but does
148+ store a (partially initialized) module object in ``sys.modules ``. To reload
149+ the module you must first :keyword: `import ` it again (this will bind the name
150+ to the partially initialized module object) before you can :func: `reload ` it.
151+
152+ When a module is reloaded, its dictionary (containing the module's global
153+ variables) is retained. Redefinitions of names will override the old
154+ definitions, so this is generally not a problem. If the new version of a
155+ module does not define a name that was defined by the old version, the old
156+ definition remains. This feature can be used to the module's advantage if it
157+ maintains a global table or cache of objects --- with a :keyword: `try `
158+ statement it can test for the table's presence and skip its initialization if
159+ desired::
160+
161+ try:
162+ cache
163+ except NameError:
164+ cache = {}
165+
166+ It is legal though generally not very useful to reload built-in or
167+ dynamically loaded modules (this is not true for e.g. :mod: `sys `,
168+ :mod: `__main__ `, :mod: `__builtin__ ` and other key modules where reloading is
169+ frowned upon). In many cases, however, extension modules are not designed to
170+ be initialized more than once, and may fail in arbitrary ways when reloaded.
171+
172+ If a module imports objects from another module using :keyword: `from ` ...
173+ :keyword: `import ` ..., calling :func: `reload ` for the other module does not
174+ redefine the objects imported from it --- one way around this is to
175+ re-execute the :keyword: `from ` statement, another is to use :keyword: `import `
176+ and qualified names (*module.name *) instead.
177+
178+ If a module instantiates instances of a class, reloading the module that
179+ defines the class does not affect the method definitions of the instances ---
180+ they continue to use the old class definition. The same is true for derived
181+ classes.
182+
183+ .. versionadded :: 3.4
184+
118185
119186:mod: `importlib.abc ` -- Abstract base classes related to import
120187---------------------------------------------------------------
0 commit comments