[Python-ideas] Allow function __globals__ to be arbitrary mapping not just dict

Robert Kern robert.kern at gmail.com
Sun Mar 18 18:47:35 CET 2012


On 3/18/12 5:26 PM, Terry Reedy wrote:
> On 3/18/2012 8:27 AM, Steven D'Aprano wrote:

>> I propose to allow function.__globals__ to accept any mapping type.
>
> The important question is whether the current ceval code in merely a holdover
> from ancient days when dict was the only mapping type and the user function type
> was not accessible to users, or whether the direct dict access is an important
> speed optimization that affects essentially all code run on cpython. If not done
> already, experiments are needed to assess the degree of slowdown.

At one time, both the locals and globals dictionaries used by exec and eval() 
were required to be true dicts, but this restriction was loosened for the locals 
mapping but not the globals. This distinction is documented in the language 
reference. You can probably find the original discussions for the reasons why 
the restriction on globals dicts remained; I don't remember the details, but it 
wasn't just an oversight.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-ideas mailing list