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.

B RAINB UILDERChapter Quiz1. How does a module source code file become a module object?2. Why might you have to set your PYTHONPATH environment variable?3. Name the four major components of the module import search path.4. Name four file types that <strong>Python</strong> might load in response to an import operation.5. What is a namespace, and what does a module’s namespace contain?Quiz Answers1. A module’s source code file automatically becomes a module object when thatmodule is imported. Technically, the module’s source code is run during theimport, one statement at a time, and all the names assigned in the processbecome attributes of the module object.2. You only need to set PYTHONPATH to import from directories other than the one inwhich you are working (i.e., the current directory when working interactively, orthe directory containing your top-level file).3. The four major components of the module import search path are the top-levelscript’s home directory (the directory containing it), all directories listed in thePYTHONPATH environment variable, standard library directories, and all directoriesin .pth path files located in standard places. Of these, programmers can customizePYTHONPATH and .pth files.4. <strong>Python</strong> might load a source code (.py) file, a byte code (.pyc) file, a C extensionmodule (e.g., a .so file on Linux or a .dll or .pyd file on Windows), or a directory ofthe same name for package imports. Imports may also load more exotic things suchas ZIP file components, Java classes under the Jython version of <strong>Python</strong>, .NETcomponents under Iron<strong>Python</strong>, and statically linked C extensions that have no filespresent at all. With import hooks, imports can load anything.5. A namespace is a self-contained package of variables, which are known as theattributes of the namespace object. A module’s namespace contains all thenames assigned by code at the top level of the module file (i.e., not nested in defor class statements). Technically, a module’s global scope morphs into the moduleobject’s attributes namespace. A module’s namespace may also be altered byassignments from other files that import it, though this is frowned upon (seeChapter 16 for more on this).Chapter Quiz | 397

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

Saved successfully!

Ooh no, something went wrong!