Docstrings

David C. Ullrich ullrich at math.okstate.edu
Sat Feb 12 13:32:28 EST 2000


Gerrit Holl wrote:

> 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
>                 [stuff about user-defined functions, including __doc__.]
>
> So this applies to all callable objects, I thought.

    The indentation would seem to suggest it applies to user-defined
functions. In any case, I don't see what you're confused about - the
part you quote says that various things _do_ have __doc__ attributes,
it says nothing to the effect that these are the _only_ things with
__doc__ attributes. Various other things do - nothing in the docs
says otherwise.

>
> 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?

    Suppose you read somewhere that a horse had four legs. Now suppose
you noticed that cats have four legs. Would that be a bug in the reference
where you read about the quadripeditude of horses?

>
> 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