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

It should result in "applejuice" as a string.

Mark Chackerian
23.8k7 gold badges116 silver badges106 bronze badges
asked Oct 8, 2009 at 20:21
4
  • 1
    See meta.stackexchange.com/questions/18584/… Commented Oct 8, 2009 at 20:39
  • 1
    From the answer you chose we can conclude that this was indeed a duplicate. Indeed, a question almost exactly the same name already existed: stackoverflow.com/questions/251464/… Commented Oct 8, 2009 at 21:45
  • I disagree that this is a duplicate of #251464 -- it seems like this question is the inverse instead. Commented Nov 10, 2012 at 19:09
  • What should it print in this case : def applejuice(): print "thefunctionname"; orangejuice = applejuice; del applejuice; orangejuice(); Commented Feb 25, 2015 at 11:52

7 Answers 7

22

This also works:

import sys
def applejuice(q):
 func_name = sys._getframe().f_code.co_name
 print func_name
answered Oct 8, 2009 at 20:33
Sign up to request clarification or add additional context in comments.

Comments

11
def applejuice(**args):
 print "Running the function 'applejuice'"
 pass

or use:

myfunc.__name__
>>> print applejuice.__name__
'applejuice'

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

answered Oct 8, 2009 at 20:27

1 Comment

this isn't from inside the function
8
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.

answered Oct 8, 2009 at 20:28

Comments

3

Another way

import inspect 
def applejuice(q):
 print inspect.getframeinfo(inspect.currentframe())[2]
answered Oct 8, 2009 at 20:31

Comments

2

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

print "applejuice"
answered Oct 8, 2009 at 20:23

2 Comments

maybe he means: def func(anothah_func): print anothah_func's name
Well, that's definitely possible. We'll see if he says what the problem is.
1

This site gave me a decent explanation of how sys._getframe.f_code.co_name works that returns the function name.

http://code.activestate.com/recipes/66062-determining-current-function-name/

answered Mar 19, 2015 at 17:59

1 Comment

Wlecome to SO! please, think about providing explanation, and not only pointing to external links.
0
def foo():
 # a func can just make a call to itself and fetch the name
 funcName = foo.__name__
 # print it
 print 'Internal: {0}'.format(funcName)
 # return it
 return funcName
# you can fetch the name externally
fooName = foo.__name__
print 'The name of {0} as fetched: {0}'.format(fooName)
# print what name foo returned in this example
whatIsTheName = foo()
print 'The name foo returned is: {0}'.format(whatIsTheName)
answered Jun 26, 2013 at 23:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.