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
1
  • 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. Commented Mar 23, 2018 at 8:28

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

def main_function(f, x):
    return f.__name__, f(x)

Comments

1

Just use __name__ to get function name as str.

def main_function(func, x):
    # do something else
    return func.__name__, func(x)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.