tags:

views:

158

answers:

2

I want to be able to do this:

def asdf():
  print __method__
"asdf"

Thanks, Noah

+4  A: 

Use the traceback module:

#!/usr/bin/env python
import traceback
def asdf():
    (filename,line_number,function_name,text)=traceback.extract_stack()[-1]
    print function_name
asdf()
unutbu
+8  A: 

You need to specify your usecase in order for us to give you a better answer. Why do you want to do this?

You can get a string with the name of a function by using __name__:

def asdf():
    print asdf.__name__
"asdf"

But, what would be the point? You might aswell just print the name directly in that case.

What would happen in this case?

def asdf():
    print __method__

foo = asdf
foo()

Print "foo" or "asdf"?

truppo