Message75829
| Author |
ssb22 |
| Recipients |
ssb22 |
| Date |
2008年11月13日.16:06:10 |
| SpamBayes Score |
0.008390986 |
| Marked as misclassified |
No |
| Message-id |
<1226592373.02.0.139711229291.issue4315@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
Here's the example code:
setting1 = "val1"
setting2 = "val2"
def dummy():
global setting1
def f(x):
d ={"setting1":setting1,"setting2":setting2}
exec(x) in d
return d['setting1'], d['setting2']
print f("setting1=setting2='new'")
Expected result: ('new', 'new')
Actual result: ('val1', 'new')
The presence of "global setting1" in a different function effectively
stops a shadowed setting1 from being created by the exec.
Workaround: Add a real assignment before the exec, i.e.:
def f(x):
setting1 = 0
exec(x)
return setting1, setting2
or do the exec in a dictionary instead of in the current scope.
Observed in:
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on Cygwin
Python 2.5.2 on 2.6.26-gentoo-r1 (by Christopher Faylor
http://www.cygwin.com/ml/cygwin/2008-11/msg00168.html )
Not observed in:
Python 2.5.1 (r251:54863, Aug 1 2008, 00:32:16) on SUSE Linux
Python 2.4.4 (#2, Apr 17 2008, 01:58:28) (Debian etch, ARM)
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22) (Ubuntu) |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2008年11月13日 16:06:13 | ssb22 | set | recipients:
+ ssb22 |
| 2008年11月13日 16:06:13 | ssb22 | set | messageid: <1226592373.02.0.139711229291.issue4315@psf.upfronthosting.co.za> |
| 2008年11月13日 16:06:12 | ssb22 | link | issue4315 messages |
| 2008年11月13日 16:06:10 | ssb22 | create |
|