0

If I want to pass a function func1 as argument in another function, but want to return the function name, what shall I do?

let say

def func1(x):
    return x**2

def main_function(func1,x):
    .....
    return ___(name of func1),value of func1(x)___

which means I want things like:

func_name, result = main_function(func1,2)

print(func_name)
func1
print(result)
4
lrh09
  • 557
  • 4
  • 13
  • However, note that functions can have more than one identifier, or none. The `__name__` will remain the first one it was assigned to, unless otherwise updated. – jonrsharpe Mar 23 '18 at 08:28

3 Answers3

3
def func1(x):
    return x**2

def main_function(f,x):
    print('f name=', f.__name__)
    print('f value=', f(x))


main_function(func1, 5)

output is

f name= func1
f value= 25
Junhee Shin
  • 748
  • 6
  • 8
1

Try this:

def main_function(f, x):
    return f.__name__, f(x)
Szabolcs
  • 3,990
  • 18
  • 38
1

Just use __name__ to get function name as str.

def main_function(func, x):
    # do something else
    return func.__name__, func(x)
Matphy
  • 1,086
  • 13
  • 21