Online Book Reader

Home Category

Learning Python - Mark Lutz [216]

By Root 1682 0

argv -- command line arguments; argv[0] is the script pathname if known

path -- module search path; path[0] is the script directory, else ''

modules -- dictionary of loaded modules

...more text omitted...

Functions, classes, and methods within built-in modules have attached descriptions in their __doc__ attributes as well:

>>> print(sys.getrefcount.__doc__)

getrefcount(object) -> integer

Return the reference count of object. The count returned is generally

one higher than you might expect, because it includes the (temporary)

...more text omitted...

You can also read about built-in functions via their docstrings:

>>> print(int.__doc__)

int(x[, base]) -> integer

Convert a string or number to an integer, if possible. A floating

point argument will be truncated towards zero (this does not include a

...more text omitted...

>>> print(map.__doc__)

map(func, *iterables) --> map object

Make an iterator that computes the function using arguments from

each of the iterables. Stops when the shortest iterable is exhausted.

You can get a wealth of information about built-in tools by inspecting their docstrings this way, but you don’t have to—the help function, the topic of the next section, does this automatically for you.

PyDoc: The help Function

The docstring technique proved to be so useful that Python now ships with a tool that makes docstrings even easier to display. The standard PyDoc tool is Python code that knows how to extract docstrings and associated structural information and format them into nicely arranged reports of various types. Additional tools for extracting and formatting docstrings are available in the open source domain (including tools that may support structured text—search the Web for pointers), but Python ships with PyDoc in its standard library.

There are a variety of ways to launch PyDoc, including command-line script options (see the Python library manual for details). Perhaps the two most prominent PyDoc interfaces are the built-in help function and the PyDoc GUI/HTML interface. The help function invokes PyDoc to generate a simple textual report (which looks much like a “manpage” on Unix-like systems):

>>> import sys

>>> help(sys.getrefcount)

Help on built-in function getrefcount in module sys:

getrefcount(...)

getrefcount(object) -> integer

Return the reference count of object. The count returned is generally

one higher than you might expect, because it includes the (temporary)

...more omitted...

Note that you do not have to import sys in order to call help, but you do have to import sys to get help on sys; it expects an object reference to be passed in. For larger objects such as modules and classes, the help display is broken down into multiple sections, a few of which are shown here. Run this interactively to see the full report:

>>> help(sys)

Help on built-in module sys:

NAME

sys

FILE

(built-in)

MODULE DOCS

http://docs.python.org/library/sys

DESCRIPTION

This module provides access to some objects used or maintained by the

interpreter and to functions that interact strongly with the interpreter.

...more omitted...

FUNCTIONS

__displayhook__ = displayhook(...)

displayhook(object) -> None

Print an object to sys.stdout and also save it in builtins.

...more omitted...

DATA

__stderr__ =

__stdin__ =

__stdout__ =

...more omitted...

Some of the information in this report is docstrings, and some of it (e.g., function call patterns) is structural information that PyDoc gleans automatically by inspecting objects’ internals, when available. You can also use help on built-in functions, methods, and types. To get help for a built-in type, use the type name (e.g., dict for dictionary, str for string, list for list). You’ll get a large display that describes all the methods available for that type:

>>> help(dict)

Help on class dict in module builtins:

class dict(object)

| dict() -> new empty dictionary.

Return Main Page Previous Page Next Page

®Online Book Reader