Defining variable after assigning them value in Py

2019-09-13 02:45发布

问题:

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.