Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>Careful though; Python “ternary” comparisons like 3<=a<=5 don’t work here.

Anyone have any idea why? I imagine if they could support it they would support it. I wonder if Python doesn't allow this kind of operator overloading?



Here's the rejected PEP for operator overloading.

https://www.python.org/dev/peps/pep-0335/

and deferred PEP for rich comparison chaining specifically

https://www.python.org/dev/peps/pep-0535/

The central issue is that `3<=a<=5` expands to `3<=a and a<=5`, and `and` coerces the variables to booleans in order to work.


>and deferred PEP for rich comparison chaining specifically

>https://www.python.org/dev/peps/pep-0535/

Oh nice! Thank you for sharing. I'm sure the functionality will eventually come.


I have been doing math in python for a year and this explains so much. I feel so stupid now. Thank you for this from self thought dev.


Apparently there're problems with overloading this. In pandas it is between(left,right), so they also didn't find a way to get it done properly.


`3 <= a <= 5` expands to `3 <= a and a <= 5`. However, based on the examples NumPy seems to require `&` instead of `and` - I’m not sure why.


A type can't implement its own 'and' and 'or' operators. (Probably because it is non-obvious how to handle their short-circuiting behaviour.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: