Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Function docstring as a local variable

Reply
Thread Tools

Re: Function docstring as a local variable

 
 
Chris Angelico
Guest
Posts: n/a
 
      07-10-2011
On Mon, Jul 11, 2011 at 3:47 AM, Andrew Berg <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Richard Thomas
Guest
Posts: n/a
 
      07-10-2011
> >>> 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
 
Reply With Quote
 
 
 
 
python@bdurham.com
Guest
Posts: n/a
 
      07-10-2011
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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      07-10-2011
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      07-10-2011
* (E-Mail Removed) <(E-Mail Removed)> [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
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      07-10-2011
On 07/10/2011 05:50 PM, Tim Johnson wrote:
> * (E-Mail Removed)<(E-Mail Removed)> [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



 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      07-11-2011
* Carl Banks <(E-Mail Removed)> [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
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      07-11-2011
On Sun, Jul 10, 2011 at 5:00 PM, Tim Johnson <(E-Mail Removed)> wrote:
> * Carl Banks <(E-Mail Removed)> [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
 
Reply With Quote
 
Tim Johnson
Guest
Posts: n/a
 
      07-11-2011
* Chris Rebert <(E-Mail Removed)> [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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does a function know the docstring of its code object? Arnaud Delobelle Python 0 09-15-2011 11:10 AM
Re: Function docstring as a local variable Carl Banks Python 5 07-11-2011 04:14 AM
Function docstring as a local variable Tim Johnson Python 5 07-11-2011 04:05 AM
docstring reference to another docstring bdb112 Python 3 03-09-2009 11:23 PM
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM



Advertisments