Docstrings
Gerrit Holl
gerrit.holl at pobox.com
Sat Feb 12 08:36:34 EST 2000
Hello,
I wrote a function help() which prints the docstring of an object
to stdout, and prints a notice otherwise. But I want to distinguish
between types without docstrings or objects without docstrings
which could have a docstring.
I found some documentation in the language reference:
http://www.python.org/doc/current/ref/types.html#l2h-97
Callable types
These are the types to which the function call operation (see
section 5.3.4, ``Calls'') can be applied:
User-defined functions
A user-defined function object is created by a function
definition (see section 7.5, ``Function definitions'').
It should be called with an argument list containing the
same number of items as the function's formal parameter
list.
Special attributes: func_doc or __doc__ is the function's
documentation string, or None if unavailable; func_name
or __name__ is the function's name; func_defaults is a
tuple containing default argument values for those
arguments that have defaults, or None if no arguments
have a default value; func_code is the code object
representing the compiled function body; func_globals is
(a reference to) the dictionary that holds the function's
global variables -- it defines the global namespace of
the module in which the function was defined. Of these,
func_code, func_defaults and func_doc (and this __doc__)
may be writable; the others can never be changed.
Additional information about a function's definition can
be retrieved from its code object; see the description of
internal types below.
So this applies to all callable objects, I thought.
But now, I noticed this:
>>> t = type('')
>>> callable(t)
0
>>> t.__doc__ # strange, no error
>>> None.__doc__
Traceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: 'None' object has no attribute '__doc__'
>>> open('/dev/null').__doc__
Traceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: __doc__
>>> [].__doc__
Traceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: __doc__
>>> f = Foo()
>>> callable(f)
0
>>> f.__doc__ # not callable but does have a docstring
'docstring'
Am I confused, is it an implementation bug or is it a documentation bug?
regards,
Gerrit.
--
Homepage: http://www.nl.linux.org/~gerrit
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O
!M !V PS+ PE? Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK----- moc.edockeeg.www//:ptth
More information about the Python-list
mailing list