tags:

views:

165

answers:

6
def applejuice(q):
   print THE FUNCTION NAME!

It should result in "applejuice" as a string.

+3  A: 

You need to explain what your problem is. Because the answer to your question is:

print "applejuice"
Lennart Regebro
maybe he means: def func(anothah_func): print anothah_func's name
wilhelmtell
Well, that's definitely possible. We'll see if he says what the problem is.
Lennart Regebro
+3  A: 
def applejuice(**args):
    print "Running the fucntion 'applejuice'"
    pass

or use:

myfunc.__name__

>>> print applejuice.__name__
'applejuice'

Also, see how-to-get-the-function-name-as-string-in-python

Casey
+4  A: 
import traceback

def applejuice(q):
   stack = traceback.extract_stack()
   (filename, line, procname, text) = stack[-1]
   print procname

I assume this is used for debugging, so you might want to look into the other procedures offered by the traceback module. They'll let you print the entire call stack, exception traces, etc.

John Millikin
+1  A: 

Another way

import inspect 
def applejuice(q):
    print inspect.getframeinfo(inspect.currentframe())[2]
JimB
+2  A: 

This also works:

import sys

def applejuice(q):
    func_name = sys._getframe().f_code.co_name
    print func_name
Jeff B
A: 

What should it print in this case :

def applejuice():
     print "thefunctionname"
orangejuice = applejuice
del applejuice
orangejuice()

?-)

bruno desthuilliers