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.

of a running <strong>Python</strong> program, and so are more global: they are picked up by everyprogram on your machine, and live on after a program completes.The import as ExtensionBoth the import and from statements have been extended to allow a module to begiven a different name in your script. The following import statement:import longmodulename as nameis equivalent to:import longmodulenamename = longmodulenamedel longmodulename# Don't keep original nameAfter such an import, you can (and in fact must) use the name listed after the as torefer to the module. This works in a from statement, too, to assign a name importedfrom a file to a different name in your script:from module import longname as nameThis extension is commonly used to provide short synonyms for longer names, andto avoid name clashes when you are already using a name in your script that wouldotherwise be overwritten by a normal import statement. It also comes in handy forproviding a short, simple name for an entire directory path when using the packageimport feature described in Chapter 20.Relative Import Syntax<strong>Python</strong> 2.5 modifies the import search path semantics of some from statements whenthey are applied to the module packages we studied in the previous chapter. Someaspects of this change will not become apparent until a later <strong>Python</strong> release (currentlythis is planned for version 2.7 and version 3.0), though some are alreadypresent today.In short, from statements can now use dots (“.”) to specify that they prefer moduleslocated within the same package (known as package-relative imports) to moduleslocated elsewhere on the module import search path (called absolute imports). That is:• Today, you can use dots to indicate that imports should be relative to the containingpackage—such imports will prefer modules located inside the package tosame-named modules located elsewhere on the import search path, sys.path.• Normal imports in a package’s code (without dots) currently default to arelative-then-absolute search path order. However, in the future, <strong>Python</strong> willmake imports absolute by default—in the absence of any special dot syntax,imports will skip the containing package itself and look elsewhere on the sys.pathsearch path.Relative Import Syntax | 431

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

Saved successfully!

Ooh no, something went wrong!