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.

The reload function expects the name of an already loaded module object, so youhave to have successfully imported a module once before you reload it. Notice thatreload also expects parentheses around the module object name, whereas importdoes not. reload is a function that is called, and import is a statement. That’s whyyou must pass the module name to reload as an argument in parentheses, and that’swhy you get back an extra output line when reloading. The last output line is justprint’s representation of the reload call’s return value, a <strong>Python</strong> module object.Functions will be discussed further in Chapter 15.The Grander Module Story: AttributesImports and reloads provide a natural program launch option because import operationsexecute files as a last step. In the broader scheme of things, though, modulesserve the role of libraries of tools, as you’ll learn in Part V. More generally, a moduleis mostly just a package of variable names, known as a namespace. The names withinthat package are called attributes—that is, an attribute is a variable name that isattached to a specific object.In typical use, importers gain access to all the names assigned at the top level of amodule’s file. These names are usually assigned to tools exported by the module—functions, classes, variables, and so on—that are intended to be used in other filesand other programs. Externally, a module file’s names can be fetched with two<strong>Python</strong> statements, import and from, as well as the reload call.To illustrate, use a text editor to create a one-line <strong>Python</strong> module file called myfile.pywith the following contents:title = "The Meaning of Life"This may be one of the world’s simplest <strong>Python</strong> modules (it contains a single assignmentstatement), but it’s enough to illustrate the basics. When this file is imported,its code is run to generate the module’s attribute. The assignment statement creates amodule attribute named title.You can access this module’s title attribute in other components in two differentways. First, you can load the module as a whole with an import statement, and thenqualify the module name with the attribute name to access it:% python # Start <strong>Python</strong>>>> import myfile # Run file; load module as a whole>>> print myfile.title # Use its attribute names: '.' to qualifyThe Meaning of LifeIn general, the dot expression syntax object.attribute lets you fetch any attributeattached to any object, and this is a common operation in <strong>Python</strong> code. Here, we’veused it to access the string variable title inside the module myfile—in other words,myfile.title.Module Imports and Reloads | 47

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

Saved successfully!

Ooh no, something went wrong!