Printing __doc__

Josh Bloom joshbloom at gmail.com
Wed Mar 21 16:08:45 EDT 2007


This will work, but I'm getting out of my depth as to whether its a good
idea to do or not.

import sys

def testFunc():
    ''' Here is my doc string'''
    n = sys._getframe().f_code.co_name
    print eval(n).__doc__

testFunc()

The issue being that n is a simply a string containing the name of the
function. You'll need the actual function itself to get its docstring.

-Josh


On 21 Mar 2007 12:47:06 -0700, gtb <goodTweetieBird at hotmail.com> wrote:
>
> 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
>     #....
>
>
> thanx,
>
> gtb
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070321/eb07115b/attachment.html>


More information about the Python-list mailing list