12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eload(changer) # Forces new code to load/run>>> changer.printer( ) # Runs the new version nowreloaded: After editingNotice that reload actually returns the module object for us—its result is usuallyignored, but because expression results are printed at the interactive prompt, <strong>Python</strong>shows a default representation.Why You Will Care: Module ReloadsBesides allowing you to reload (and hence rerun) modules at the interactive prompt,module reloads are also useful in larger systems, especially when the cost of restartingthe entire application is prohibitive. For instance, systems that must connect to serversover a network on startup are prime candidates for dynamic reloads.They’re also useful in GUI work (a widget’s callback action can be changed while theGUI remains active), and when <strong>Python</strong> is used as an embedded language in a C or C++program (the enclosing program can request a reload of the <strong>Python</strong> code it runs, withouthaving to stop). See Programming <strong>Python</strong> for more on reloading GUI callbacks andembedded <strong>Python</strong> code.More generally, reloads allow programs to provide highly dynamic interfaces. Forinstance, <strong>Python</strong> is often used as a customization language for larger systems—userscan customize products by coding bits of <strong>Python</strong> code on-site, without having torecompile the entire product (or even having its source code at all). In such worlds, the<strong>Python</strong> code already adds a dynamic flavor by itself.To be even more dynamic, though, such systems can automatically reload the <strong>Python</strong>customization code periodically at runtime. That way, users’ changes are picked upwhile the system is running; there is no need to stop and restart each time the <strong>Python</strong>code is modified. Not all systems require such a dynamic approach, but for those thatdo, module reloads provide an easy-to-use dynamic customization tool.Chapter SummaryThis chapter delved into the basics of module coding tools—the import and fromstatements, and the reload call. We learned how the from statement simply adds anextra step that copies names out of a file after it has been imported, and how reloadforces a file to be imported again with stopping and restarting <strong>Python</strong>. We also surveyednamespace concepts, saw what happens when imports are nested, exploredthe way files become module namespaces, and learned about some potential pitfallsof the from statement.412 | Chapter 19: Module Coding Basics

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!