04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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, to<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 to 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 Python programmer, and I couldn’t possibly cover all of them here. The current<br />

standard library is large enough to 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 to 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 to investigate it in the Python interpreter. The first<br />

thing you need to do is to 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 to<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 to 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 “to 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!