15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lines 13<br />

The Unix startup line is followed by the module documentation string. Keep your documentation string<br />

simple yet descriptive enough to be useful. Ours is a bit short, but so is this script. (We invite the reader<br />

to take a look at the documentation string at the commencement of the cgi module in the standard<br />

library for a seriously lengthy example of module documentation.)<br />

Lines 56<br />

We import the operating system (os) module next, and in line 6, we create a new local alias for the<br />

linesep attribute of that module. By doing this, we can shorten the name of the variable and also speed<br />

up access to it.<br />

Lines 814<br />

<strong>Core</strong> Tip: Use local variables to substitute for module attributes<br />

Names like os.linesep require the interpreter to do two lookups: (1)<br />

lookup os to find that it is a module, and (2) look up the linesep<br />

attribute of that module. Because modules are also global variables,<br />

we pay another penalty. If you use an attribute like this often in a<br />

function, we recommend you alias it to a single local variable. Lookups<br />

are much fasterlocal variables are always searched first before globals,<br />

and we don't have attribute lookups either. This is one of the tricks in<br />

making your programs faster: replace often-used (and name-lengthy)<br />

module attributes with local references. Your code runs faster and has<br />

less clutter with a shorter name.<br />

In our code snippet, we do not have a function to show you an<br />

example of using a local alias. Instead, we have a global alias, which is<br />

halfway there. At least we do not have to perform two lookups to get<br />

to the object.<br />

If it is not apparent already, this is an "infinite loop," meaning we are presented with a body of code that<br />

will repeat and run forever unless we exit the looplook for a break statement somewhere! The while true<br />

conditional causes this to happen because while statements execute whenever its conditional expression<br />

evaluates to Boolean true, and true is Boolean true.<br />

Lines 1014 prompt the user for an unused filename, meaning that the filename entered should not be<br />

the name of an already existing file. The raw_input() built-in function takes an argument to use as the<br />

prompt to the user. The resulting string entered by the user is the return value of raw_input(), which in<br />

this case gets assigned to fname.<br />

If the user is unlucky enough to pick a name already in use, we notify the user and return the user to<br />

the prompt to enter another (file)name. Note that os.path.exists() is a helper function in the os.path<br />

(sub)module, which helps us make this determination. Only when a file with such a name does not exist,

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

Saved successfully!

Ooh no, something went wrong!