[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
if __name__ == '__main__':
import sys # when run, not imported
more(open(sys.argv[1]).read(), 10) # page contents of file on cmdline

When the more.py file is imported, we pass an explicit string to its more function, and
this is exactly the sort of utility we need for documentation text. Running this utility
on the sys module’s documentation string gives us a bit more information in human-
readable form about what’s available to scripts:


C:\...\PP4E\System> python
>>> from more import more
>>> import sys
>>> more(sys.__doc__)
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.

Dynamic objects:

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

displayhook -- called to show results in an interactive session
excepthook -- called to handle any uncaught exception other than SystemExit
To customize printing in an interactive session or to install a custom
top-level exception handler, assign other functions to replace these.

stdin -- standard input file object; used by input()
More?

Pressing “y” or “Y” here makes the function display the next few lines of documenta-
tion, and then prompt again, unless you’ve run past the end of the lines list. Try this
on your own machine to see what the rest of the module’s documentation string looks
like. Also try experimenting by passing a different window size in the second
argument—more(sys.doc, 5) shows just 5 lines at a time.


Python Library Manuals


If that still isn’t enough detail, your next step is to read the Python library manual’s
entry for sys to get the full story. All of Python’s standard manuals are available online,
and they often install alongside Python itself. On Windows, the standard manuals are
installed automatically, but here are a few simple pointers:



  • On Windows, click the Start button, pick All Programs, select the Python entry
    there, and then choose the Python Manuals item. The manuals should magically
    appear on your display; as of Python 2.4, the manuals are provided as a Windows
    help file and so support searching and navigation.

  • On Linux or Mac OS X, you may be able to click on the manuals’ entries in a file
    explorer or start your browser from a shell command line and navigate to the library
    manual’s HTML files on your machine.


System Scripting Overview | 85
Free download pdf