Sep-06-2020, 05:08 AM
The suggestion with lambda goes against PEP8 recommendations:
as to OP question - function is object as any other, you can always assign the function to variable:
also, you may be interested in functools.partial
Quote:Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier:
# Correct: def f(x): return 2*x # Wrong: f = lambda x: 2*xthe first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)
as to OP question - function is object as any other, you can always assign the function to variable:
def spam(): print('This is spam') eggs = spam eggs()This has plenty of useful applications, e.g. the pythonic way to create a menu/switch statement, using dict with functions as values.
also, you may be interested in functools.partial
from functools import partial def raise_power(num, power): return num ** power power_3 = partial(raise_power, power=3) print(power_3(2))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs