Op 2005-12-16, Peter Otten schreef <[EMAIL PROTECTED]>: > Antoon Pardon wrote: > >> I have the following little piece of code: >> >> class Cfg:pass >> #config = Cfg() >> >> def assign(): >> setattr(config, 'Start' , [13, 26, 29, 34]) >> >> def foo(): >> config = Cfg() >> dct = {'config':config, 'assign':assign} >> exec "assign()" in dct >> print config.Start >> >> foo() >> >> >> When I execute this I get the following error: >> >> Traceback (most recent call last): >> File "mod1.py", line 13, in ? >> foo() >> File "mod1.py", line 10, in foo >> exec "assign()" in dct >> File "<string>", line 1, in ? >> File "mod1.py", line 5, in assign >> setattr(config, 'Start' , [13, 26, 29, 34]) >> NameError: global name 'config' is not defined >> >> Now I don't understand this. In the documentation I read the following: >> >> If only the first expression after in is specified, it should be a >> dictionary, which will be used for both the global and the local >> variables. >> >> I provided a dictionary to be used for the global variables and it >> contains a 'config' entry, so why doesn't this work? > > > If you have a module > > v = 42 > def f(): return v > > > and call f in another module > > print f() > > where no global variable v is defined, would you expect that call to fail? > Of course not, but how can f look up the variable v then?
But I am not just calling. I'm using exec. And from the documentation from exec I get the impression you can use it so that a function will have temporarily a different reference to global namespace. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list