0% found this document useful (0 votes)
3 views1 page

Learning_Python (1)

The reload function in Python allows for the reloading and rerunning of a module's code, enabling immediate observation of changes without stopping the entire program, thus shortening the development cycle. It is particularly useful for long-running applications, such as database programs, that can update themselves without needing to reconnect on each change. In Python 2.6, reload is a built-in function, while in Python 3.0, it is part of the imp module and requires an import statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Learning_Python (1)

The reload function in Python allows for the reloading and rerunning of a module's code, enabling immediate observation of changes without stopping the entire program, thus shortening the development cycle. It is particularly useful for long-running applications, such as database programs, that can update themselves without needing to reconnect on each change. In Python 2.6, reload is a built-in function, while in Python 3.0, it is part of the imp module and requires an import statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

• The reload function forces an already loaded module’s code to be reloaded and

rerun. Assignments in the file’s new code change the existing module object
in-place.
Why all the fuss about reloading modules? The reload function allows parts of a pro-
gram to be changed without stopping the whole program. With reload, therefore, the
effects of changes in components can be observed immediately. Reloading doesn’t help
in every situation, but where it does, it makes for a much shorter development cycle.
For instance, imagine a database program that must connect to a server on startup;
because program changes or customizations can be tested immediately after reloads,
you need to connect only once while debugging. Long-running servers can update
themselves this way, too.
Because Python is interpreted (more or less), it already gets rid of the compile/link steps
you need to go through to get a C program to run: modules are loaded dynamically
when imported by a running program. Reloading offers a further performance ad-
vantage by allowing you to also change parts of running programs without stopping.
Note that reload currently only works on modules written in Python; compiled exten-
sion modules coded in a language such as C can be dynamically loaded at runtime, too,
but they can’t be reloaded.

Version skew note: In Python 2.6, reload is available as a built-in func-


tion. In Python 3.0, it has been moved to the imp standard library
module—it’s known as imp.reload in 3.0. This simply means that an
extra import or from statement is required to load this tool (in 3.0 only).
Readers using 2.6 can ignore these imports in this book’s examples, or
use them anyhow—2.6 also has a reload in its imp module to ease mi-
gration to 3.0. Reloading works the same regardless of its packaging.

reload Basics
Unlike import and from:
• reload is a function in Python, not a statement.
• reload is passed an existing module object, not a name.
• reload lives in a module in Python 3.0 and must be imported itself.
Because reload expects an object, a module must have been previously imported suc-
cessfully before you can reload it (if the import was unsuccessful, due to a syntax or
other error, you may need to repeat it before you can reload the module). Furthermore,
the syntax of import statements and reload calls differs: reloads require parentheses,
but imports do not. Reloading looks like this:
import module # Initial import
...use module.attributes...
... # Now, go change the module file
...

Reloading Modules | 555

You might also like