Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9176 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2011-07-11 04:11 +1000 |
| Last post | 2011-07-10 18:14 -0500 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Function docstring as a local variable Chris Angelico <rosuav@gmail.com> - 2011-07-11 04:11 +1000
Re: Function docstring as a local variable Richard Thomas <chardster@gmail.com> - 2011-07-10 14:16 -0700
Re: Function docstring as a local variable python@bdurham.com - 2011-07-10 18:13 -0400
Re: Function docstring as a local variable Roy Smith <roy@panix.com> - 2011-07-10 18:41 -0400
Re: Function docstring as a local variable Tim Johnson <tim@johnsons-web.com> - 2011-07-10 14:50 -0800
Re: Function docstring as a local variable Tim Chase <python.list@tim.thechases.com> - 2011-07-10 18:14 -0500
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-07-11 04:11 +1000 |
| Subject | Re: Function docstring as a local variable |
| Message-ID | <mailman.844.1310321477.1164.python-list@python.org> |
On Mon, Jul 11, 2011 at 3:47 AM, Andrew Berg <bahamutzero8825@gmail.com> 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
[toc] | [next] | [standalone]
| From | Richard Thomas <chardster@gmail.com> |
|---|---|
| Date | 2011-07-10 14:16 -0700 |
| Message-ID | <c5669e17-290e-455c-975d-5f659129e6b4@d7g2000vbv.googlegroups.com> |
| In reply to | #9176 |
> >>> 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
[toc] | [prev] | [next] | [standalone]
| From | python@bdurham.com |
|---|---|
| Date | 2011-07-10 18:13 -0400 |
| Message-ID | <mailman.852.1310336017.1164.python-list@python.org> |
| In reply to | #9181 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2011-07-10 18:41 -0400 |
| Message-ID | <roy-346B3C.18415110072011@news.panix.com> |
| In reply to | #9186 |
In article <mailman.852.1310336017.1164.python-list@python.org>,
python@bdurham.com 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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-10 14:50 -0800 |
| Message-ID | <mailman.857.1310338213.1164.python-list@python.org> |
| In reply to | #9181 |
* python@bdurham.com <python@bdurham.com> [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
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2011-07-10 18:14 -0500 |
| Message-ID | <mailman.861.1310339713.1164.python-list@python.org> |
| In reply to | #9181 |
On 07/10/2011 05:50 PM, Tim Johnson wrote: > * python@bdurham.com<python@bdurham.com> [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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web