On Thu, 17 Sep 2020 at 20:07, Chris Angelico <ros...@gmail.com> wrote: > > On Fri, Sep 18, 2020 at 4:55 AM Alex Hall <alex.moj...@gmail.com> wrote: > > > > On Thu, Sep 17, 2020 at 8:49 PM Chris Angelico <ros...@gmail.com> wrote: > >> > >> The only time you can safely mutate locals() is when you're at top > >> level and it's the same as globals(). > > > > > > It's safe in class definitions, right? At least in CPython it seems to > > work. I've done that a few times, most recently to dynamically generate > > enum members. > > > > Hmm. I'm not actually sure there, but my belief is that it still isn't > guaranteed - it just happens to work in current versions of CPython. > > locals() > Return a dictionary containing the current scope's local variables. > > NOTE: Whether or not updates to this dictionary will affect name lookups > in > the local scope and vice-versa is *implementation dependent* and not > covered by any backwards compatibility guarantees.
Correct, it's not guaranteed behaviour. "It seems to work" is as good as you'll get. PEP 558 (not yet accepted) proposes to standardise the behaviour. See the PEP for more detail. Paul _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/I3KN5HUE3KM32OIL7M6AJDMVLD7NKRVE/ Code of Conduct: http://python.org/psf/codeofconduct/