Printing __doc__

gtb goodTweetieBird at hotmail.com
Wed Mar 21 16:52:56 EDT 2007


On Mar 21, 3:35 pm, Peter Otten <__pete... at web.de> wrote:
> 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

Thanks.

Pardon my ignorance, but brittle?




More information about the Python-list mailing list