mikefallopian
mikefallopian

Reputation: 221

Overloading "==" operator for numpy arrays

I am defining a function in Python that needs to check

if a==b:
  do.stuff()

In principle, a and b could be numpy arrays or integers, and I would like my implementation to be robust against this. However, to check equality for a numpy array, one needs to append the boolean with all(), which will break the code when a and b are integers.

Is there a simple way to code the equality test so that it works regardless of whether a and b are integers or numpy arrays?

Upvotes: 1

Views: 202

Answers (1)

Ehsan
Ehsan

Reputation: 12407

how about this that works for both arrays and integers(numbers):

if np.array_equal(a,b):
    do.stuff()

Upvotes: 1

Related Questions