Saturday, November 9, 2013

Python: Get function name programaticaly


Share at Facebook

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debugging prints from your program.


import sys

def new_function():
    function_name = sys._getframe().f_code.co_name
    print function_name

new_function()



You of course can write the function name manually(function_name="new_function"). But that case if you change the function's name, you have to change the string as well from the variable function_name.





No comments: