Examining names

Cat Spray No More

Stop your Cat Spraying in the House

Get Instant Access

When working in interactive mode, you sometimes need to be reminded what names you've given to objects. The dir() function, which is built into interactive mode, lists the names (such as names of data objects, module names, and function names) that are stored in the interactive mode's namespace at any particular point in your coding session. (Namespace is a Python term for a list of names that a particular part of a program knows about.)

Tip You can also use the dir() function to examine the contents of modules. Examining the namespace

The following example shows what happens when you start Python's interactive mode (so you have not defined anything yet), use dir() to see what is defined, and then give a value to a name and use dir() again:

% python

Python 2.5b1 (r25b1:47038M, Jun 20 2006, 16:17:55) >>> dir()

[' builtins ', ' doc ', ' name '] >>> too many cats = "Impossible!" >>> dir()

After you give a value the name too_many_cats, the namespace remembers that name and gives you the value if you ask for it, like this:

Was this article helpful?

0 0

Post a comment