Printing __doc__

gtb goodTweetieBird at hotmail.com
Wed Mar 21 16:37:09 EDT 2007


On Mar 21, 3:11 pm, "Terry Reedy" <tjre... at udel.edu> wrote:
> "gtb" <goodTweetieB... at hotmail.com> wrote in message
>
> news:1174506426.108098.243940 at d57g2000hsg.googlegroups.com...
> | Greetings,
> |
> | Don't know the daily limit for dumb questions so I will ask one or
> | more.
> |
> | 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
> |    #....
>
> The docstring you are looking for is attached to the *function* object as
> .__doc__ and .func_doc.  Frame.f_code is a *code* object.  It has a
> boilerplate doc string, but not the one you want.  As near as I can tell,
> frames do not keep references to the func object but only the code object,
> which is all it needs to run the code.
>
> I believe tracebacks use co_filename and co_name to find the text of a
> function.  You could try to parse out the doc string from there.
>
> Terry Jan Reedy

Thanks for posting.

OK, .__doc__ or .func_doc

But still the question remains. I cannot use
  print .__doc__
OR
  print .func_doc

within the function.





More information about the Python-list mailing list