On 12.03.20 16:02, Eric Wieser wrote
`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
This is not surprising since the user deliberately created the temporary
variable. It's not the responsibility of Numpy to optimize that away (in
fact it can't since `bc` might be reused later on).
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`
If I wanted to split the computation over multiple lines and yet have it
optimized I would just reuse the same (target) name instead of creating
a temporary one and then discarding it in the next step:
a = b * c
a += d
If there's many lines to the computation one can place a comment at the
top, indicating that the result (`a`) is built incrementally in order to
prevent confusion about names.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/3VDDZ5QYJ3BND2F64OVU4YEWES2YO4YE/
Code of Conduct: http://python.org/psf/codeofconduct/