Home > OS >  TypeError: 'int' object is not callable when trying to check if any array elements meet a
TypeError: 'int' object is not callable when trying to check if any array elements meet a

Time:11-17

I have a numpy array of integers. If any of them are greater than threshold I want it to evaluate as true.

import numpy as np

arr = np.arange(10)
threshold = 5

if arr.any() > threshold:
    print(arr)

TypeError: 'int' object is not callable states I shouldn't name a variable and a function the same thing, I don't think I'm doing that. In the example in the accepted answer round is being used as a variable name and as its function.

I tried changing the code in case arr or threshold is a function I'm not aware of and can't find on google to:

import numpy as np

a = np.arange(10)
b = 5

if a.any() > b():
    print(a)

But I get the same error. How do I return true if any array elements are > threshold?

Full Traceback error:

Traceback (most recent call last):
  File "c:\Users\test.py", line 6, in <module>
    if a.any() > b():
TypeError: 'int' object is not callable

CodePudding user response:

Hey you're using the any() function wrong.

Switch from

if arr.any() > threshold:
    print(arr)

to

if np.any(arr > threshold):
    print(arr)
  • Related