I think it's a micro-optimization that's probably not worth it for most code and more likely to occasionally disappoint people who are using the debugger.

Once we have a JIT mode or other super-optimization mode it can be done.

On Tue, Oct 20, 2020 at 7:32 AM Serhiy Storchaka <storchaka@gmail.com> wrote:
It is common to use _ as a placeholder for variable whose value is not
used. For example:

    for _ in range(n)
    head, _, tail = name.partition(':')
    first, *_, last = items

I though about optimizing out unnecessary assignments. Actually I wrote
a patch half year ago and tested it. It did not add much to performance,
and did not reduce the bytecode, so I ccoled down to him and put it off.
Later PEP 622 was declared the use of _ in pattern matching, so I was
waiting for what it would come to. And now PEP 640 is created to solve
the same problem.

My patch was too conservative. It was limited to local variables and
underscored names. For global variables we can't determine if the
variable is not used. And eliminating unused non-underscored variables
breaks too many tests (mainly for debugger, tracing, etc).

I was not sure whether it should be limited to underscored names or just
'_' (I seen also uses of '__' as a drop out variable in wild).

Maybe we can extend this to global '_'. Global '_' is used as a holder
for the last result in REPL and as an alias to gettext (this is the
reason of PEP 640), but none of them is actually set in the assignment
statement. You can use `globals()['_'] = ...` or `globals().update({'_':
...})` or `sys.modules[__name__]._ = ...` to set global '_'.

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?
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UF22D3FOJIA4JAHEV4FPMZLIG6XE7V3X/
Code of Conduct: http://python.org/psf/codeofconduct/

--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)