Python: Retrieve Function Name Without Execution

  • Context: Python 
  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Minor Python
Click For Summary
SUMMARY

The discussion centers on retrieving the name of a function in Python without executing it. The user inquired about printing the function name for a defined function, specifically "apple". The solution provided is to use the attribute func_name on the function object, allowing users to access the function's name directly with print(apple.func_name). This method is applicable to Python 2.x versions, as func_name is deprecated in Python 3.x.

PREREQUISITES
  • Understanding of Python function definitions
  • Familiarity with Python 2.x and 3.x differences
  • Basic knowledge of Python object attributes
  • Experience with Python syntax and print statements
NEXT STEPS
  • Research the differences between Python 2.x and 3.x regarding function attributes
  • Learn about the __name__ attribute in Python 3.x for retrieving function names
  • Explore Python's introspection capabilities for deeper insights into function objects
  • Study best practices for function documentation and naming conventions in Python
USEFUL FOR

Python developers, educators teaching Python programming, and anyone interested in function introspection and naming conventions in Python.

zeion
Messages
455
Reaction score
1

Homework Statement



Hi there,

I just have a quick one here..
Is there a way to bring the NAME of a function? Not execute it?

Like if I had

def apple():
return

Can I get it to print "apple" somehow?
Thanks

Homework Equations





The Attempt at a Solution

 
Technology news on Phys.org
Nevermind I found the answer.
In case anyone else is curious you can do

print <function>.func_name
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K