Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10934

Re: Get the name of a function

References <8295aba8-3eab-4c6e-b3b7-c3421d829220@f7g2000vba.googlegroups.com>
Date 2011-08-05 14:19 -0700
Subject Re: Get the name of a function
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.1947.1312579168.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Aug 5, 2011 at 11:52 AM, gervaz <gervaz@gmail.com> wrote:
> Hi all, is there a way to retrive the function name like with
> self.__class__.__name__?
>
> Using self.__dict__.__name__ I've got
>
>>>> def test():
> ...     print(self.__dict__.__name__)
> ...

Er, where did `self` magically come from?

>>>> test
> <function test at 0x0178DDF8>
>
> But I really just want the function name, so 'test'
>
> Any help?

Courtesy of the "Observations on the three pillars of Python execution" thread:

from inspect import currentframe

def foobar():
    my_name = currentframe().f_code.co_name
    print(my_name)

I would recommend against a function knowing its own name though,
unless it's for debugging or necessary metaprogramming purposes.

Cheers,
Chris

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Get the name of a function gervaz <gervaz@gmail.com> - 2011-08-05 11:52 -0700
  Re: Get the name of a function Emile van Sebille <emile@fenx.com> - 2011-08-05 13:19 -0700
  Re: Get the name of a function Chris Rebert <clp2@rebertia.com> - 2011-08-05 14:19 -0700
  Re: Get the name of a function Grant Edwards <invalid@invalid.invalid> - 2011-08-05 22:05 +0000

csiph-web