I'm reading this tutorial and under the Returning Functions part, there's an example something like below:
def parent(n):
def child1():
return "Printing from the child1() function."
def child2():
return "Printing from the child2() function."
if n == 10: return child1
else: return child2
The author mentions that the return functions should not have parenthesis in them but without giving any detailed explanation. I believe that it is because if parenthesis are added, then the function will get called and in some way the flow will be lost. But I need some better explanation to get a good understanding.
If you add parenthesis i.e. ()
to the return function then you will be returning the return-value of that function (i.e. the function gets executed and its result is returned). Otherwise, you are returning a reference to that function that can be re-used. That is,
f = parent(1)
f() # executes child2()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With