
20.10.20 20:13, Steven D'Aprano пише:
On Tue, Oct 20, 2020 at 05:30:54PM +0300, Serhiy Storchaka wrote:
I do not want to create tens of alternate PEPs with minor variations, and this issue is not worth a PEP. What is your opinion about this? Is it worth to include such optimization? For what kind of variables should it be applied? Should it include global '_'? Should it be merely an optimization (maybe controlled by the -O option) or change in the language?
Assuming that this actually has a benefit, in either speed or memory, how about this?
And this is a large IF.
1. Only eliminate local variables, never globals.
2. By default, only variables with a leading underscore are eliminated. This will(?) avoid breaking tests for the debugger etc. that you mentioned.
It is actually what my patch does.
3. Under -O, allow more aggressive optimization that eliminates non-underscore variables too.
It is easy to add, but as Guido noted it will break the code that uses locals(). I though about eliminating underscore variables only with -O. It would make the feature safer, but even less useful.
4. This is an implementation feature, not a language promise. Other interpreters do not have to follow.
It is a question about which I am not sure.
5. To be clear, variables are only eliminated if they are assigned to, but never written to:
Yes. And for implementation simplification "del" counts as the use of the variable. So _ is not eliminated in the following code: a, *_ = foo() del _