[Python-ideas] Allow function __globals__ to be arbitrary mapping not just dict
Steven D'Aprano
steve at pearwood.info
Tue Mar 20 04:01:50 CET 2012
On Sun, Mar 18, 2012 at 11:27:09PM +1100, Steven D'Aprano wrote:
> I propose to allow function.__globals__ to accept any mapping type.
Jython already supports this behaviour.
steve at runes:~$ jython
[...]
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> class Mapping:
... def __getitem__(self, key):
... if key == 'a': return 1
... else: raise KeyError(key)
...
>>> from types import FunctionType
>>> f = lambda x: x + a
>>> g = FunctionType(f.func_code, Mapping(), 'g')
>>> g(2)
3
--
Steven
More information about the Python-ideas
mailing list