On Mon, Aug 20, 2018 at 12:43 AM, Kirill Balunov <kirillbalu...@gmail.com> wrote: > > чт, 16 авг. 2018 г. в 22:37, Chris Barker via Python-ideas > <python-ideas@python.org>: >> >> >> I wonder why locals doesn't return a Mapping Proxy, or other read-only >> mapping object? >> >> If it's not guaranteed to be THE locals dict, and changes *may* not affect >> the real one (but may), a read-only object seems like much safer idea. >> >> -CHB >> > > I have the same feeling and already have asked the same question before > https://mail.python.org/pipermail/python-list/2018-February/731241.html. The > main take away was that it is the CPython implementation detail and that > `MappingProxy` type was introduced only in 3.3. I still do not find these > arguments convincing. On the other hand with the current status quo - > `locals` returns `dict` inside a function, we have only advantages: > > a) `locals` always returns a dict and is consistent in all places. (To be > consistent is always good.) > > b) This type of question occurs every 6 months (there is always a topic to > talk about at ML). > > c) When someone encounters this behavior first time, he thinks that he found > a bug <- There was no error but nothing happened (gives an opportunity to > study the internal details of the CPython implementation, the average level > increases. Everyone also tries to find a way, something like "I believe, > there must be a way, because Python is so dynamic" which theoretically > allows you to make even more new discoveries.). > > d) This is considered by some as a way not to leak the CPython > implementation details. (Here I have no comments, let it be on the > conscience of others.) > > e) It leaves a room for a future changes (In fact, in some situations I > would like to have this possibility).
Related to (e) is that there is room for other implementations to permit changes to locals(), and furthermore, a fully-compliant Python implementation may use an actual dictionary for locals, and simply return that. (That CPython doesn't is an implementation detail for the sake of performance.) Requiring that it be a proxy would impose unnecessary cost on the implementation, without benefiting any compliant use. ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/