3
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 :)

2
  • which python version you're using? I'm getting an error for 1st expression in python 3.6
    – Sociopath
    Commented Feb 20, 2019 at 10:29
  • @AkshayNevrekar the first expression works in python 2.7.14
    – Aemyl
    Commented Feb 20, 2019 at 10:32

1 Answer 1

2

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.