getting name of the function you're in

Steven Majewski sdm7g at Virginia.EDU
Thu May 16 18:45:48 EDT 2002


On 15 May 2002, Bengt Richter wrote:

> 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')
>

Also, no need for the split() or the backquotes -- try instead:

	sys._getframe().f_code.co_name

-- Steve M.







More information about the Python-list mailing list