So this would break many uses of locals(). I'm not sure I like that.

On Tue, Oct 20, 2020 at 10:16 AM Steven D'Aprano <> wrote:
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?

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

3. Under -O, allow more aggressive optimization that eliminates
non-underscore variables too.

4. This is an implementation feature, not a language promise. Other
interpreters do not have to follow.

5. To be clear, variables are only eliminated if they are assigned to,
but never written to:

    def func():
        _a = 1  # not eliminated
        _b = 2  # eliminated
        return _a

Python-ideas mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct:

--Guido van Rossum (
Pronouns: he/him (why is my pronoun here?)