[Python-Dev] Overloading comparison operator for lists

Rob Cliffe rob.cliffe at btinternet.com
Tue May 28 21:52:09 EDT 2019



On 27/05/2019 04:52:17, Montana Burr wrote:
> NumPy arrays have this awesome feature, where array == 3 does an 
> element-wise comparison and returns a list. For example:
>
> np.array([1,2,3,4,5])==3
>
> returns
>
> [False,False,True,False,False]
>
> It would be cool if Python had similar functionality for lists.
>
Well, it does have:
 >>>  [x==3 for x in [1,2,3,4,5]]
[False, False, True, False, False]

This is IMHO much more intuitive than your construct overloading "==".
It is also more flexible (any operation can be performed on x, not just 
an equality comparison).
So sorry, but I can see no justification for changing the Python 
language to do something, which can already be done, to be done in a 
more obscure way.
Also
     [1,2,3,4,5]==3
is already legal syntax (it evaluates to False, since the operands are 
not equal), so you are proposing a code-breaking change.

Apologies if someone has already pointed all this out, as is very likely 
(I have only just (re-)joined python-ideas and may have missed the 
relevant posts).
Best wishes
Rob Cliffe


More information about the Python-Dev mailing list