>>> ================================ RESTART ================================
>>>
20144208
100000
1
100000
Traceback (most recent call last):
File "C:\Python34\test.py", line 12, in <module>
print(y)
NameError: name 'y' is not defined
>>>
def test():
print(id(x))
print(x)
if __name__=='__main__':
x='e f t efdad'
test()
然后在idle里面运行
>>> ================================ RESTART ================================
>>>
31843176
e f t efdad
>>> ================================ RESTART ================================
>>>
31843136
e f t efdad
>>>
def test():
global x
y=3
def test2():
print(y)
x=2
z=1
a='a'
b='b'
if __name__=='__main__':
x='e f t efdad'
test()
上面的代码,x是全局变量,y是非本地变量,其他的z、a、b都是本地变量
>>> x
2
>>> y
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
y
NameError: name 'y' is not defined
>>> a
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
a
NameError: name 'a' is not defined
b
>>> c
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
c
NameError: name 'c' is not defined
>>> z
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
z
NameError: name 'z' is not defined
>>>