local variables in exec

Jeremy Lowery jslowery at hotmail.com
Mon Dec 17 09:23:29 CET 2001


a snipplet would be describe this.
>>> x = 100
>>> def f():
...  print x
...
>>> f()
100
>>> lns = {x: '4500'}
>>> fc = 'def f(): print x'
>>> bc = compile(fc, '', 'exec')
>>> exec bc in lns
>>> lns['f']()
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "", line 1, in f
NameError: global name 'x' is not defined
>>>

why doesn't this work?







More information about the Python-list mailing list