Printing __doc__
Peter Otten
__peter__ at web.de
Wed Mar 21 16:35:29 EDT 2007
gtb wrote:
> In a function I can use the statement n =
> sys._getframe().f_code.co_name to get the name of the current
> function. Given that I can get the name how can I print the __doc__
> string? I cant use the following, it will tell me to bugger off as the
> string has no such attribute.
>
> def spam(self):
> n = sys._getframe().f_code.co_name
> print n.__doc__ #Wrong
> print __doc__ #No good either
> #....
>>> import sys
>>> def docstring():
... return sys._getframe(1).f_code.co_consts[0]
...
>>> def foo():
... print "My docstring is %r" % docstring()
...
>>> def bar():
... "bar's docstring"
... print "My docstring is %r" % docstring()
...
>>> foo()
My docstring is None
>>> bar()
My docstring is "bar's docstring"
No idea how brittle that might be, though.
Peter
More information about the Python-list
mailing list