
On Mon, Dec 27, 2021 at 07:19:12PM -0000, Stefan Pochmann wrote:
Stephen J. Turnbull wrote:
In fact you also created a whole new subordinate data flow that doesn't exist in the original (the [x+1]). I bet that a complex comprehension in your style will need to create a singleton iterable per source element for every mapping except the first.
I don't think so. Sounds like you missed that `for x in [x + 1]` is now treated as `x = x + 1` not only by humans but also by Python, see the first item here:
I'm afraid that is doubly wrong. Humans still read `for x in [x + 1]` as a loop, because that's what it is. So says at least this human. And Python the language makes no promise about that being optimized into a simple assignment. Only CPython 3.9 and above does so. Other implementations may or may not do so, and being a mere optimization, it could be removed at any time without notice if it were found to be interfering with some other feature or more powerful optimization. Optimizations are great, but we must be careful not to treat them as language features unless they are documented as such. None is a singleton and always will be, so it is safe (and recommended!) to test `is None`. But 211 may or may not be a singleton, and so testing `is 211` is risky, even it it happens to work for you under some circumstances. Caching of small ints is an implementation-dependent optimization, not a language feature. And so is the comprehension inner loop speed-up. You can rely on it being fast if you like, but that ties you to a specific implementation and version. -- Steve