Asked  7 Months ago    Answers:  5   Viewed   34 times

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

def my_function():

print get_function_name_as_string(my_function) # my_function is not in quotes

should output "my_function".

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



Using __name__ is the preferred method as it applies uniformly. Unlike func_name, it works on built-in functions as well:

>>> import time
>>> time.time.func_name
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'builtin_function_or_method' object has no attribute 'func_name'
>>> time.time.__name__ 

Also the double underscores indicate to the reader this is a special attribute. As a bonus, classes and modules have a __name__ attribute too, so you only have remember one special name.

Tuesday, June 1, 2021
answered 7 Months ago

You could use get_defined_vars() to find the name of a variable that has the same value as the one you're trying to find the name of. Obviously this will not always work, since different variables often have the same values, but it's the only way I can think of to do this.

Edit: get_defined_vars() doesn't seem to be working correctly, it returns 'var' because $var is used in the function itself. $GLOBALS seems to work so I've changed it to that.

function print_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;

    return false;

Edit: to be clear, there is no good way to do this in PHP, which is probably because you shouldn't have to do it. There are probably better ways of doing what you're trying to do.

Wednesday, March 31, 2021
answered 9 Months ago
def rotate(letter, rot):
    shift = 97 if letter.islower() else 65
    return chr((ord(letter) + rot - shift) % 26 + shift)

letter = input('Enter a letter: ')
rot = int(input('Enter a number: '))
print(rotate(letter, rot))
Tuesday, October 19, 2021
Kevin S.
answered 2 Months ago

This also works:

import sys

def applejuice(q):
    func_name = sys._getframe().f_code.co_name
    print func_name
Sunday, October 31, 2021
Matthias Bäßler
answered 1 Month ago

You can zip the lists to iterate through them in an element-wise fashion

for func, param in zip(string1, string2):

For example

string1 = [len, type, max]
string2 = ['hello', 5, [1,3,7]]
for func, param in zip(string1, string2):


<class 'int'>

Also note that in your list of functions you should not add () to the end of the function because you will call the function if you do so. Just leave the function name itself (see the above string1 list for example).

For your code the loop would look like

for each, every in zip(string2, string1):
Tuesday, December 7, 2021
answered 2 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :