12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

object. As we saw in the last part of this book, imports give access to names in amodule’s global scope. That is, the module file’s global scope morphs into the moduleobject’s attribute namespace when it is imported. Ultimately, <strong>Python</strong>’s modulesallow us to link individual files into a larger program system.More specifically, from an abstract perspective, modules have at least three roles:Code reuseAs discussed in Chapter 3, modules let you save code in files permanently.Unlike code you type at the <strong>Python</strong> interactive prompt, which goes away whenyou exit <strong>Python</strong>, code in module files is persistent—it can be reloaded and rerunas many times as needed. More to the point, modules are a place to definenames, known as attributes, that may be referenced by multiple external clients.System namespace partitioningModules are also the highest-level program organization unit in <strong>Python</strong>. Fundamentally,they are just packages of names. Modules seal up names into self-containedpackages, which helps avoid name clashes—you can never see a name in anotherfile, unless you explicitly import that file. In fact, everything “lives” in a module—code you execute and objects you create—are always implicitly enclosed inmodules. Because of that, modules are natural tools for grouping systemcomponents.Implementing shared services or dataFrom an operational perspective, modules also come in handy for implementingcomponents that are shared across a system, and hence require only a singlecopy. For instance, if you need to provide a global object that’s used by morethan one function or file, you can code it in a module that can then be importedby many clients.For you to truly understand the role of modules in a <strong>Python</strong> system, though, we needto digress for a moment, and explore the general structure of a <strong>Python</strong> program.<strong>Python</strong> Program ArchitectureSo far in this book, I’ve sugarcoated some of the complexity in my descriptions of<strong>Python</strong> programs. In practice, programs usually involve more than just one file; forall but the simplest scripts, your programs will take the form of multifile systems.And even if you can get by with coding a single file yourself, you will almost certainlywind up using external files that someone else has already written.This section introduces the general architecture of <strong>Python</strong> programs—the way youdivide a program into a collection of source files (a.k.a. modules) and link the partsinto a whole. Along the way, we’ll also explore the central concepts of <strong>Python</strong> modules,imports, and object attributes.386 | Chapter 18: Modules: The Big Picture

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

Saved successfully!

Ooh no, something went wrong!