This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: String executed inside a function ignores global statements
Type: behavior Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Qwert225, xiang.zhang
Priority: normal Keywords:

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) * (Python committer) 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:36adminsetgithub: 72251
2016-09-10 19:10:57xiang.zhangsetstatus: open -> closed

nosy: + xiang.zhang
messages: + msg275676

resolution: not a bug
stage: resolved
2016-09-10 17:14:52Qwert225create