[Tutor] Print Doc Strings

Evgeny Roubinchtein eroubinc@u.washington.edu
Tue, 5 Oct 1999 18:21:57 -0700 (PDT)


On Tue, 5 Oct 1999 mike@virtualblue.org wrote:

>After trying your loop and then trying to display a __doc__ string it
>looks like I'm not dealing with void __doc__ strings correctly.

So why not put it in a "try" statement like so
try:
    if item.__doc__ is not None:
        print item.__doc__
except AttributeError:
    pass

Would this work?

>
>$ python
>Python 1.5.1 (#1, Mar 21 1999, 22:49:36)  [GCC egcs-2.91.66 19990314/Li on linux-i386
>Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>> import sys
>>>> for name in dir(sys):
>...   item = getattr(sys,name)
>...   print item.__doc__
>... 
>Traceback (innermost last):
>  File "<stdin>", line 3, in ?
>AttributeError: 'None' object has no attribute '__doc__'
>>>> 
>
>>>> dir(sys)
>['__doc__', '__name__', '__stderr__', '__stdin__', '__stdout__', 'argv', 'builtin_module_names', 'copyright', 'exc_info', 'exc_type', 'exec_prefix', 'executable', 'exit', 'getrefcount', 'last_traceback', 'last_type', 'last_value', 'maxint', 'modules', 'path', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile', 'settrace', 'stderr', 'stdin', 'stdout', 'version']
>>>> sys.__doc__
>>>> print sys.__doc__
>None
>>>> sys.path.__doc__
>Traceback (innermost last):
>  File "<stdin>", line 1, in ?
>AttributeError: __doc__
>>>> dir(sys.path)
>['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort']
>>>> dir(sys.path.append)
>['__doc__', '__name__', '__self__']
>>>> dir(sys.path.append.__doc__)
>[]
>>>> print sys.path.append.__doc__
>None
>>>> 
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://www.python.org/mailman/listinfo/tutor
>
>

--
Evgeny Roubinchtein, eroubinc@u.washington.edu
...................
EBO: Emulate Brown-Out