parsed.org

Tips by tag: semantics

Dynamic Docstrings by Hawk-McKain on May 05, 2008 12:02 AM

Apparently Python only recognizes a doc string a litteral string placed after a function definition, meaning it won't accept an interpolated string, or even 'a'+'b', for whatever reason. For example:

>>> def doc():
...     '''Useful info. Note: %s''' % 'You'll never see this.'
...     print doc.__doc__
>>> doc()
None

To work around this you must explicitly set __doc__. Example:

>>> def doc():
...     doc.__doc__ = '''Useful info. Note: %s''' % 'Bacon is yummy!'
...     print doc.__doc__
>>> doc()
Useful info. Note: Bacon is yummy!
docstringpythonsemanticsstringssyntax
RSS