TypeError: ‘str’ object is not callable, which means we can not use a string to be a function. In this tutorial, we will write an example to show you how to fix this type error.
Here is an python callback example.
def filter(x): if x % 2 == 0: return x return -1 def compute(x, filter_fun): result = [] for i in x: j = filter_fun(x) if j > 0 : result.append(j) return result x = [1,2,3,4,5] result = compute(x, 'filter') print(result)
In this example, code below will report a type error.
result = compute(x, 'filter')
The reason is string filter is not a function, it can not be called like a function.
To fix this error, we should use a function name like:
result = compute(x, filter)
Then the result is:
[2, 4]
This type error is fixed.