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.

• Modules should rarely change other modules’ variables. We illustrated thiswith code in Chapter 16, but it’s worth repeating here: it’s perfectly OK to useglobals defined in another module (that’s how clients import services, after all),but changing globals in another module is often a symptom of a design problem.There are exceptions, of course, but you should try to communicate resultsthrough devices such as function argument return values, not cross-modulechanges. Otherwise, your globals’ values become dependent on the order of arbitrarilyremote assignments in other files, and your modules become harder tounderstand and reuse.As a summary, Figure 21-1 sketches the environment in which modules operate.Modules contain variables, functions, classes, and other modules (if imported).Functions have local variables of their own. You’ll meet classes—other objects thatlive within modules—in Chapter 22.ModulesVariablesFunctionsVariablesClassesMembersMethodsimportOther modules(<strong>Python</strong> or C)VariablesFunctionsClasses/TypesimportOther modules(<strong>Python</strong> or C)VariablesFunctionsClasses/TypesFigure 21-1. Module execution environment. Modules are imported, but modules also import anduse other modules, which may be coded in <strong>Python</strong> or another language such as C. Modules in turncontain variables, functions, and classes to do their work, and their functions and classes maycontain variables and other items of their own. At the top, though, programs are just a set ofmodules.Modules Are Objects: MetaprogramsBecause modules expose most of their interesting properties as built-in attributes, it’seasy to write programs that manage other programs. We usually call such manager programsmetaprograms because they work on top of other systems. This is also referred toas introspection because programs can see and process object internals. Introspection isan advanced feature, but it can be useful for building programming tools.Module Design Concepts | 435

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

Saved successfully!

Ooh no, something went wrong!