Function docstring as a local variable

Chris Angelico rosuav at gmail.com
Sun Jul 10 14:11:14 EDT 2011


On Mon, Jul 11, 2011 at 3:47 AM, Andrew Berg <bahamutzero8825 at 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



More information about the Python-list mailing list