Re: Function docstring as a local variable

Discussion in 'Python' started by Chris Angelico, Jul 10, 2011.

  1. On Mon, Jul 11, 2011 at 3:47 AM, Andrew Berg <> wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: RIPEMD160
    >
    > On 2011.07.10 12:41 PM, Tim Johnson wrote:
    >> It possible for a function to print it's own docstring?
    >>>> def test():

    > ...     """Hi there."""
    > ...     print(test.__doc__)


    That's assuming that it knows its own name, and that that name hasn't
    been rebound. Is there a way for a function to find its own self?

    >>> def findself():

    """Find myself. Ooh look, there I am!"""
    import sys
    try:
    1/0
    except:
    traceback=sys.exc_info()[2]
    # Now I'm not sure what to do with traceback.
    # traceback.tb_frame.f_code.co_name is the function name ("findself").
    # Is there a way to get the function object?

    I'm kinda half-way there, but I've never worked with traceback
    objects. Someone will know, I'm sure!

    ChrisA


    ChrisA
     
    Chris Angelico, Jul 10, 2011
    #1
    1. Advertising

  2. > >>> def findself():
    >
    >         """Find myself. Ooh look, there I am!"""
    >         import sys
    >         try:
    >                 1/0
    >         except:
    >                 traceback=sys.exc_info()[2]
    >         # Now I'm not sure what to do with traceback.
    >         # traceback.tb_frame.f_code.co_name is the function name ("findself").
    >         # Is there a way to get the function object?


    I'm pretty sure there isn't. I've tried a number of times before to
    find it but failed. Fundamentally frame objects don't need to know
    about functions. Functions are just one way of wrapping code objects
    but sometimes code objects come in modules.

    You can use sys._getframe() to get the current frame instead of the
    traceback.

    Richard
     
    Richard Thomas, Jul 10, 2011
    #2
    1. Advertising

  3. Chris Angelico

    Guest

    I'm not sure how a function can get a generic handle to itself, but if
    you're willing to hardcode the function name, then this technique works:

    def test():
    """This is my doc string"""
    print test.__doc__

    test()

    Outputs:

    This is my doc string

    Malcolm
     
    , Jul 10, 2011
    #3
  4. Chris Angelico

    Roy Smith Guest

    In article <>,
    wrote:

    > I'm not sure how a function can get a generic handle to itself, but if
    > you're willing to hardcode the function name, then this technique works:
    >
    > def test():
    > """This is my doc string"""
    > print test.__doc__
    >
    > test()
    >
    > Outputs:
    >
    > This is my doc string
    >
    > Malcolm


    I'm sure there has to be a cleaner way that this, but one possible way
    for a function to find its name is to catch an exception and look at the
    traceback:

    ---------------------------------------
    #!/usr/bin/env python

    import sys
    import traceback

    def foo():
    "The Larch"
    try:
    raise Exception
    except Exception, ex:
    _, _, tb = sys.exc_info()
    stacks = traceback.extract_tb(tb)
    file_name, line_number, function_name, text = stacks[0]
    print "I am %s", function_name
    print "My docstring is", eval(function_name).__doc__

    foo()
    --------------------------------------

    This works, but yuck.
     
    Roy Smith, Jul 10, 2011
    #4
  5. Chris Angelico

    Tim Johnson Guest

    * <> [110710 14:17]:
    > I'm not sure how a function can get a generic handle to itself, but if
    > you're willing to hardcode the function name, then this technique works:
    >
    > def test():
    > """This is my doc string"""
    > print test.__doc__
    >
    > test()

    Works for me. Works for the application I'm after. thanks
    Here's a related question:
    I can get the docstring for an imported module:
    >>> import tmpl as foo
    >>> print(foo.__doc__)

    Python templating features

    Author - tim at akwebsoft dot com

    ## Is it possible to get the module docstring
    ## from the module itself?

    Thanks again
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
     
    Tim Johnson, Jul 10, 2011
    #5
  6. Chris Angelico

    Tim Chase Guest

    On 07/10/2011 05:50 PM, Tim Johnson wrote:
    > * <> [110710 14:17]:
    >> def test():
    >> """This is my doc string"""
    >> print test.__doc__
    >> test()

    >
    > Works for me. Works for the application I'm after. thanks
    > Here's a related question:
    > ## Is it possible to get the module docstring
    > ## from the module itself?


    You're gonna kick yourself :)

    print __doc__

    -tkc
     
    Tim Chase, Jul 11, 2011
    #6
  7. Chris Angelico

    Tim Johnson Guest

    * Carl Banks <> [110710 15:18]:
    > On Sunday, July 10, 2011 3:50:18 PM UTC-7, Tim Johnson wrote:
    > > Here's a related question:
    > > I can get the docstring for an imported module:
    > > >>> import tmpl as foo
    > > >>> print(foo.__doc__)

    > > Python templating features
    > >
    > > Author - tim at akwebsoft dot com
    > >
    > > ## Is it possible to get the module docstring
    > > ## from the module itself?

    >
    >
    > print __doc__

    Thanks Carl.

    Where is general documentation on the subject of variables
    beginning with 2 underscores?

    I'm presuming the key phrase is 'builtin variables'. I'm searching
    too ...

    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
     
    Tim Johnson, Jul 11, 2011
    #7
  8. Chris Angelico

    Chris Rebert Guest

    On Sun, Jul 10, 2011 at 5:00 PM, Tim Johnson <> wrote:
    > * Carl Banks <> [110710 15:18]:
    >> On Sunday, July 10, 2011 3:50:18 PM UTC-7, Tim Johnson wrote:

    <snip>
    >> >  ## Is it possible to get the module docstring
    >> >  ## from the module itself?

    >>
    >> print __doc__

    >  Thanks Carl.
    >
    >  Where is general documentation on the subject of variables
    >  beginning with 2 underscores?
    >
    >  I'm presuming the key phrase is 'builtin variables'. I'm searching
    >  too ...


    I've never heard that phrase used to describe __doc__ or its friends.

    Look in the "underscore" section of the documentation index:
    http://docs.python.org/genindex-_.html

    Cheers,
    Chris
     
    Chris Rebert, Jul 11, 2011
    #8
  9. Chris Angelico

    Tim Johnson Guest

    * Chris Rebert <> [110710 16:14]:
    > >
    > >  Where is general documentation on the subject of variables
    > >  beginning with 2 underscores?

    >
    > I've never heard that phrase used to describe __doc__ or its friends.

    :) That why I wasn't satified with my search results.
    > Look in the "underscore" section of the documentation index:
    > http://docs.python.org/genindex-_.html

    And that is what I was looking for.
    thanks
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
     
    Tim Johnson, Jul 11, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sairam
    Replies:
    2
    Views:
    369
    Steve Holden
    Apr 5, 2007
  2. bdb112
    Replies:
    3
    Views:
    263
    Chris Rebert
    Mar 9, 2009
  3. Tim Johnson

    Function docstring as a local variable

    Tim Johnson, Jul 10, 2011, in forum: Python
    Replies:
    5
    Views:
    213
    alex23
    Jul 11, 2011
  4. Carl Banks
    Replies:
    5
    Views:
    192
    alex23
    Jul 11, 2011
  5. Arnaud Delobelle
    Replies:
    0
    Views:
    203
    Arnaud Delobelle
    Sep 15, 2011
Loading...

Share This Page