James Thiele a écrit :
> I'd like to access the name of a function from inside the function. My
> first idea didn't work.
> 
> 
>>>>def foo():
> 
> ...     print func_name
> ...
> 
>>>>foo()
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 2, in foo
> NameError: global name 'func_name' is not defined
> 
> My second attempt works but looks ugly.
> 
> 
>>>>def foo():
> 
> ...     import inspect
> ...     print inspect.stack()[0][3]
> ...
> 
>>>>foo()
> 
> foo
> 
> Is there a standard way of getting the name of a function from inside
> the function?
> 

You've already got good answers. Now here's another workaround:

class _FooFunction(object):
   def __call__(self):
     print self.__class__.__name__
foo = _FooFunction()

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to