局部变量会覆盖全局变量的值,此外,局部变量在脱离其作用域时失效。
>>> a=3
>>> def hello();
SyntaxError: invalid syntax
>>> def hello():
a=5
print(a)
>>> hello()
5
>>> a
3
>>> def hello():
b=5
print(a)
>>> hello()
3
>>> b
Traceback (most recent call last):
File "<pyshell#60>", line 1, in <module>
b
NameError: name 'b' is not defined
>>> globals().keys()
dict_keys(['a', 'MIMEMultipart', 'encoders', '__builtins__', 'utils', 'time', 'MIMEBase', '__doc__', 'sys', 'pdb', 'logname', 'hello', 'output', '__name__', '__package__', 'os', 'email', 'COMMASPACE'])