0

Is it possible to call a function which is passed as a parameter in Python? I have tried doing it, but it gives an error

Code:

def doStuff():
    print("doing stuff")

def doWork():
    print("doing work")

def call_function(func):
    func()

call_function(doStuff())
call_function(doWork())

Error:

TypeError: 'NoneType' object is not callable
PradMaster
  • 83
  • 10

3 Answers3

5

call_function(doStuff()) means "use the value returned by doStuff as the argument to call_function". This is not what you want, you want to use the function doStuff itself as the argument.

To get the effect you want, omit the parentheses on the function you're passing as an argument: call_function(doStuff)

The key insight here is the distinction between the function itself and the value returned by the function. The function itself is an entity in python, like a string or a list or a dictionary, which has the property of being callable. (that is, it has a __call__ method) When you see a reference to a function followed by a matched pair of parentheses, either by name (as in doStuff()) or by alias (as in func()) or as a reference (as in lambda x: x+1()) that expression evaluates to the value returned by the function. This value is None unless the execution of the function hits a return statement, in which case the returned value is just the result of evaluating the expression following the return. The value returned by the function can be any legitimate python entity, including another function.

All of these words, which bear thinking about and possibly further experimentation or study on your part, allow me to say more simply that doStuff is the name of a function, and doStuff() is an expression which triggers the execution of that function and evaluates to the returned value of that function

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • Basically, he needs to pass a function into `call_function` not a value. But the `doStuff()` returns a value. – Raihan Kabir Dec 08 '20 at 15:37
  • oh..I did not know that. Thanks for the help – PradMaster Dec 08 '20 at 15:51
  • I was just wondering if the function which has to be called needs a few arguments...how can we do that?? – PradMaster Dec 12 '20 at 07:43
  • This is worth a new question. Short answer: when you have any reference to a function, you can call it with whatever arguments it needs. However, there are interesting options if you want to go down the path of functional programming in python. – Jon Kiparsky Dec 13 '20 at 04:04
1

Your argument for the call_function function should just pass the function name, not the function call, e.g. call_function(doStuff)

Robert King
  • 974
  • 5
  • 16
0
def doStuff():
    print("doing stuff")

def doWork():
    print("doing work")

def call_function(func):
    func()

call_function(doStuff)
call_function(doWork)
abc
  • 28
  • 1
  • 11
  • Welcome to StackOverflow. For future reference, I find it's most helpful to explain the phenomenon in question, rather than simply providing the correct incantation. – Jon Kiparsky Dec 08 '20 at 16:44