Reputation: 763
from operator import ge
import numpy as np
>>> ge([0,2,3], 0.8)
True
>>> ge([0,2,3], np.float64(0.8))
array([False, True, True])
Any explanation for the difference in the behaviour?
Later found out:
>>> ge([0,2,3], np.float(0.8))
True
too :)
Upvotes: 3
Views: 72
Reputation: 107357
It's obviously the difference between the way that Python and Numpy deal with arrithmatic operations. ge(a, b)
is the same as a >= b
. If one of the operands is a Numpy object the corresponding method will be called and if the other operand is an array it will perform the comparison in an element-wise manner. That is, if you do the following you'll get the same result:
In [3]: [0,2,3] >= np.float64(0.8)
Out[3]: array([False, True, True])
Python in other hand, deals with the situation differently in both 2 and 3 versions. You're presumably using Python-2.X and it has it's related logic (almost ilogical lol) but in python 3 you can't compare objects in different types, unless the respective operation is explicitly implemented for one of the objects. For built-in objects except different numeric types, objects with different types never compare equal.
In [4]: ge([0,2,3], 0.8)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-405eded6881c> in <module>()
----> 1 ge([0,2,3], 0.8)
TypeError: '>=' not supported between instances of 'list' and 'float'
Read https://docs.python.org/3/library/stdtypes.html#comparisons for more details.
Upvotes: 2