Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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)
  • 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__)

image

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...