This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Python: exec in a function can't create shadows of variables if these are declared "global" in another function of the same module


Consider this Python code:

setting1 = "val1"
setting2 = "val2"

def dummy():
    global setting1

def f(x):
    exec(x)
    return setting1, setting2

print f("setting1='new' ; setting2='new'")

Expected result: ('new', 'new')

Actual result: ('val1', 'new')

The presence of "global setting1" in a totally
different function somehow stops a shadowed
setting1 from being created by the exec.

This can be worked around by adding a real
assignment before the exec, i.e.:

def f(x):
    setting1 = 0
    exec(x)
    return setting1, setting2

Observed in:
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on Cygwin

Not observed in:
Python 2.5.1 (r251:54863, Aug  1 2008, 00:32:16) on SUSE Linux

Silas


-- 
Silas S Brown http://people.pwf.cam.ac.uk/ssb22

"If your axe is dull and you don't sharpen it,
 you have to work harder to use it." - Ecclesiastes 10:10 (TEV)

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]