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.

B RAINB UILDERChapter Quiz1. What is the purpose of an _ _init_ _.py file in a module package directory?2. How can you avoid repeating the full package path every time you reference apackage’s content?3. Which directories require _ _init_ _.py files?4. When must you use import instead of from with packages?Quiz Answers1. The _ _init_ _.py file serves to declare and initialize a module package; <strong>Python</strong>automatically runs its code the first time you import through a directory in aprocess. Its assigned variables become the attributes of the module object createdin memory to correspond to that directory. It is also not optional—youcan’t import through a directory with package syntax, unless it contains this file.2. Use the from statement with a package to copy names out of the packagedirectly, or use the as extension with the import statement to rename the path toa shorter synonym. In both cases, the path is listed in only one place, in the fromor import statement.3. Each directory listed in an import or from statement must contain an _ _init_ _.pyfile. Other directories, including the directory containing the leftmost componentof a package path, do not need to include this file.4. You must use import instead of from with packages only if you need to access thesame name defined in more than one path. With import, the path makes the referencesunique, but from allows only one version of any given name.Chapter Quiz | 425

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

Saved successfully!

Ooh no, something went wrong!