function name

Tim Chase python.list at tim.thechases.com
Wed Apr 28 15:20:28 EDT 2010


On 04/28/2010 01:50 PM, Emile van Sebille wrote:
> On 4/28/2010 11:44 AM Richard Lamboj said...
>> is there any way to get the name from the actual called function, so that the
>> function knows its own name?
>
>   >>>  def test():pass
> ...
>   >>>  dir(test)
> ['__call__', '__class__', '__closure__', '__code__', '__defaults__',
> '__delattr_
> _', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__',
> '__globa
> ls__', '__hash__', '__init__', '__module__', '__name__', '__new__',
> '__reduce__'
> , '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
> '__subcla
> sshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict',
> 'func_doc'
> , 'func_globals', 'func_name']
>   >>>  test.func_name
> 'test'

Just be aware of the oddities that go along with asking for this...

   >>> def foo(): pass
   ...
   >>> bar = foo
   >>> bar.func_name
   'foo'
   >>> (lambda x: x).func_name
   '<lambda>'

-tkc






More information about the Python-list mailing list