
On Thu, 12 Mar 2020 at 15:06, Eric Wieser <wieser.eric+numpy@gmail.com> wrote:
will print the same id three times. However, when used as a list comprehension, the optimization can't step in, and I have no way of using the `del` keyword
>>> [id(v) for v in itertools.combinations([1, 2, 3], 1)] [2500926200992, 2500926199072, 2500926200992]
You can get the same effect by not naming the value being thrown away:
list(map(id, itertools.combinations([1,2,3], 1))) [2267883926192, 2267883926192, 2267883926192]
`itertools.combinations` is not the only place to make this optimization - parts of numpy use it too, allowing
a = (b * c) + d
to elide the temporary `b*c`. This elision can't happen with the spelling
bc = b * c a = bc + d
My suggestion would be to make `del x` an expression, with semantics "unbind the name `x`, and evaluate to its value". This would allow:
>>> [id(del v) for v in itertools.combinations([1, 2, 3], 1)] [2500926200992, 2500926200992, 2500926200992]
and
bc = b * c a = (del bc) + d # in C++, this would be `std::move(bc) + d`
But why would you want this anyway? Do you have an example of an application where this sort of micro-optimisation is significant enough to justify a fairly major language change? (And yes, I know the optimisation in itertools could just as easily be dismissed as "why is this needed?", but that didn't need a language change to implement...) Paul