Defining variable after assigning them value in Python 2.7

烂漫一生 提交于 2019-12-25 08:28:46

问题


I get this out put

=============================== RESTART: Shell ===============================
>>> 
Warning (from warnings module):
  File "E:/Python/Roy Progs/test.py", line 2
    global x
SyntaxWarning: name 'x' is assigned to before global declaration
>>> 
==================== RESTART: E:/Python/Roy Progs/test.py ====================
10
>>> 

when I run this code.

x=5
global x
x=10
print x

Yes I know that defining a variable after assigning it a value is absurd, however python seems to understand the code. I have 2 questions: 1) Why does Python give a warning and not an error. 2) why is there a restart after the warning message.

It would be helpful to know how exactly python is interpreting this code. thanks in advance.

来源:https://stackoverflow.com/questions/42023636/defining-variable-after-assigning-them-value-in-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!