access __doc__ from within function without reference to function name
Duncan Booth
duncan.booth at invalid.invalid
Tue Oct 2 13:32:02 EDT 2007
Tim Chase <python.list at tim.thechases.com> wrote:
> The parser also has to accomodate "raw" and "unicode" string
> prefixes, as they're valid too:
>
> def f(x):
> r"raw!"
> pass
>
> def f(x):
> u"Unicode"
> pass
>
>
> in addition. Okay...in most of these cases, the pathological
> coder should be taken out back and beaten, but it's a non-trivial
> problem :)
Fortunately someone already wrote a parser for Python code, so it is pretty
trivial:
>>> import compiler
>>> class Visitor(compiler.visitor.ASTVisitor):
def visitFunction(self, node):
print node.doc
>>> def showdoc(source):
compiler.walk(compiler.parse(source), Visitor())
>>> showdoc(r'''def f(x):
"""this is a \"""docstring\"""
with \\\"""and\""" mutliple lines"""
pass
''')
this is a """docstring"""
with \"""and""" mutliple lines
>>> showdoc(r'''def f(x):
u"Unicode" " with concatenation"
pass
''')
Unicode with concatenation
>>>
More information about the Python-list
mailing list