Issue 28064: String executed inside a function ignores global statements (original) (raw)
Issue28064
Created on 2016-09-10 17:14 by Qwert225, last changed 2022-04-11 14:58 by admin. This issue is now closed.
| Messages (2) | ||
|---|---|---|
| msg275657 - (view) | Author: (Qwert225) | Date: 2016-09-10 17:14 |
| String executed inside a function ignores global statements stated before string execution. See the example below - the global variable value should be changed to 'newText' by the function, but is not. Example: variable = 'text' def changeVariable(): global variable exec("variable = 'newText'") changeVariable() print(str(variable)) | ||
| msg275676 - (view) | Author: Xiang Zhang (xiang.zhang) * ![]() |
Date: 2016-09-10 19:10 |
| Not a bug. Put the global statement inside: >>> variable = 'test' >>> def changeVariable(): ... exec("global variable; variable = 'newText'") ... >>> changeVariable() >>> print(variable) newText |
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:36 | admin | set | github: 72251 |
| 2016-09-10 19:10:57 | xiang.zhang | set | status: open -> closednosy: + xiang.zhangmessages: + resolution: not a bugstage: resolved |
| 2016-09-10 17:14:52 | Qwert225 | create |
