16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

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.

CHAPTER 10 ■ BATTERIES INCLUDED 211<br />

After the first statement (1), the contents of the __init__ module in drawing would be<br />

available; the drawing and colors modules, however, would not be. After the second statement<br />

(2), the colors module would be available, but only under its full name, drawing.colors. After<br />

the third statement (3), the shapes module would be available, under its short name (that is,<br />

simply shapes). Note that these statements are just examples. There is no need, for example, <strong>to</strong><br />

import the package itself before importing one of its modules as I have done here. The second<br />

statement could very well be executed on its own, as could the third. You may nest packages<br />

inside each other.<br />

Exploring Modules<br />

Before I tackle some of the standard library modules, I’ll show you how <strong>to</strong> explore modules on<br />

your own. This is a valuable skill because you will encounter lots of useful modules in your<br />

career as a <strong>Python</strong> programmer, and I couldn’t possibly cover all of them here. The current<br />

standard library is large enough <strong>to</strong> warrant books all by itself (and such books have been<br />

written)—and it’s growing. New modules are added with each release, and often some of the<br />

modules undergo slight changes and improvements. Also, you will most certainly find several<br />

useful modules on the Web, and being able <strong>to</strong> grok them quickly and easily will make your<br />

programming much more enjoyable. 1<br />

What’s in a Module?<br />

The most direct way of probing a module is <strong>to</strong> investigate it in the <strong>Python</strong> interpreter. The first<br />

thing you need <strong>to</strong> do is <strong>to</strong> import it, of course. Let’s say you’ve heard rumors about a standard<br />

module called copy:<br />

>>> import copy<br />

No exceptions are raised—so it exists. But what does it do? And what does it contain?<br />

Using dir<br />

To find out what a module contains, you can use the dir function, which lists all the attributes<br />

of an object (and therefore all functions, classes, variables, and so on of a module). If you try <strong>to</strong><br />

print out dir(copy), you get a long list of names. (Go ahead, try it.) Several of these names begin<br />

with an underscore—a hint (by convention) that they aren’t meant <strong>to</strong> be used outside the module.<br />

So let’s filter them out with a little list comprehension (check the section on list comprehension<br />

in Chapter 5 if you don’t remember how this works):<br />

>>> [name for name in dir(copy) if name[0] != '_']<br />

['Error', 'PyStringMap', 'copy', 'deepcopy', 'error']<br />

The list comprehension is the list consisting of all the names from dir(copy) that don’t<br />

have an underscore as their first letter. This list is much less confusing than the full listing.<br />

1. The term “grok” is hackerspeak, meaning “<strong>to</strong> understand fully,” taken from Robert A. Heinlein’s novel<br />

Stranger in a Strange Land (Ace Books, reissue 1995).

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

Saved successfully!

Ooh no, something went wrong!