logoalt Hacker News

gpugregyesterday at 11:19 AM1 replyview on HN

Because the use case is very niche and nobody optimized it yet.

https://github.com/python/cpython/issues/135824#issuecomment...

`x in range(n)` is already optimized, but that was easier since the `__contains__` method already existed, but an equivalent `__min__` or `__max__` does not.


Replies

zahlmanyesterday at 7:28 PM

Man, I proposed the idea of `__min__`/`__max__` (and a few others) in 2023[0], exactly because of this kind of big-O optimization potential, and it was poorly received: https://discuss.python.org/t/_/25095

Another idea[1] that I won't get official credit for, I guess. Which, you know, I was raised in the "ideas are nothing, implementation is everything" era of code, but it still hurts.

(Edit: I confused myself into thinking they were actually implementing the optimization in 3.16; they are not, or at least there's no evidence of it at present. Regardless, the hesitancy to implement this sort of improvement is rather irritating to me. See also https://github.com/python/cpython/issues/90716 .)

By the way, `x in range(n)` is only optimized for integer `x`. Not for nonconvertible types (where the answer should obviously just be False) and not for floating-point (values equal to an integer have to get converted and checked O(N) times, and other values can't be immediately rejected). That's been proposed and poorly received before too: https://discuss.python.org/t/_/18248 [2].

[0]: and I'd first thought of it long before that and didn't know where to propose it, plus it kept slipping my mind

[1]: like https://zahlman.github.io/posts/a-brief-annotation/

[2]: see also my later post there, which went ignored

show 1 reply