getting name of the function you're in

Bengt Richter bokr at oz.net
Wed May 15 18:56:26 EDT 2002


On Wed, 15 May 2002 14:59:55 -0600, "Andrew Dalke" <dalke at dalkescientific.com> wrote:

>Richard Townsend:
>>    try:
>>        raise ZeroDivisionError
>>    except ZeroDivisionError:
>>        # Get the traceback object
>>        tb = sys.exc_info()[2]
>
>As mentioned in this newsgroup in other threads, I've learned
>a bunch of old habits which need to change because of newer
>tricks in Python.
>
>sys._getframe()
>
Looks like no need for raising exception either:

 >>> import sys
 >>> def foo():
 ...    def inside_foo():
 ...       return `sys._getframe().f_code`.split()[2]
 ...    return `sys._getframe().f_code`.split()[2],inside_foo()
 ...
 >>> foo()
 ('foo', 'inside_foo')



Regards,
Bengt Richter



More information about the Python-list mailing list