Using instance as __doc__

Seo Sanghyeon unendliche at hanmail.net
Wed Jun 4 22:04:21 EDT 2003


On Python 2.2.2 on Win XP, I tried the following:

----
class MyDoc:
  def __str__(self):
    return "This is a documentation."

def my_function():
  pass

my_function.__doc__ = MyDoc()
help(my_function)
----

I hope my intent here is clear.

I expected to see the online help, but failed. After digging the source,
I found that help() uses inspect.getdoc() to get docstrings.

----
# inspect.py around line 270

try:
    doc = object.__doc__
except AttributeError:
    return None
if not isinstance(doc, (str, unicode)):
    return None
----

So... Python throws out __doc__ if it is not a real string. Why not use
something like "doc = str(doc)" instead? Yeah, quite useless, perhaps.
I have no idea of possible use cases. But if there is some hidden reason
for not to use str() on instance __doc__, please enlighten me.

-- Seo Sanghyeon




More information about the Python-list mailing list