
On Thu, 12 Mar 2020 at 14:09, Paul Moore <p.f.moore@gmail.com> wrote:
On Thu, 12 Mar 2020 at 16:57, Eric Wieser <wieser.eric+numpy@gmail.com> wrote:
It looks like actually this can be be built as a function today:
def move(name): return inspect.currentframe().f_back.f_locals.pop(name)
Which works as follows, but it feels awkward to pass variable names by strings (and will confuse linters):
>>> for v in itertools.combinations([1, 2, 3], 1): ... print(id(move("v"))) 1718903397008 1718903397008 1718903397008
I'm both impressed and horrified by this :-)
Even - it just works because it is on the top-level scope of a module - and would not work if called from a function, where `locals()` modification actually do not affect the local variables. (And that behavior that was sort of "in the air" was well documented and defined prior to Python 3.8 in PEP 558) But for the module-level-scope locals() == globals() is just a plain dictionary, not one working as a proxy to slotted local variables (And in a class body, locals() is watever is returned from the metaclass `__prepare__` method )
Paul _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/42KOI2... Code of Conduct: http://python.org/psf/codeofconduct/