Update locals()
holger krekel
pyth at devel.trillke.net
Sat Apr 27 15:57:25 EDT 2002
Hello,
how can you dynamically update the locals() namebindings?
Example what does *not* work:
def func(somedict):
locals().update(somedict)
print i
func({'i': None})
This fails on 'print i' with
NameError: global name 'i' is not defined
dissassembled bytecode with 'import dis ; dis.dis(func)':
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_GLOBAL 0 (locals)
9 CALL_FUNCTION 0
12 LOAD_ATTR 1 (update)
15 LOAD_FAST 0 (somedict)
18 CALL_FUNCTION 1
21 POP_TOP
22 SET_LINENO 3
25 LOAD_GLOBAL 3 (i)
28 PRINT_ITEM
29 PRINT_NEWLINE
30 LOAD_CONST 0 (None)
33 RETURN_VALUE
Did the compiler decide [25] that 'i' must mean a global
binding? Is there an elegant way to get around this?
thanks,
holger
More information about the Python-list
mailing list