Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

In Python, how do I get a function name as a string, without calling the function?

def my_function(): 

pass print 

get_function_name_as_string(my_function) # my_function is not in quotes

should output "my_function".

Is such a function available in Python? If not, any ideas on how to implement get_function_name_as_string, in Python?

1 Answer

0 votes
by (106k points)
edited by
  • In Python, every function has a property called ‘.__name__’ attached to it. We can access this property and get the name of a function. 

  • For example:

def abc_func():

    return 0

print (abc_func.__name__)


Learn more about Python from an expert. Enroll in our Python Course!

Browse Categories