python局部变量与全局变量

name = "head first python"
def what_happens_here():
    print(name)  1
    name = "python"  2
    name = name + "is a great book!"
    print(name)
what_happens_here()
print(name)

 

这里行1 会出错,原因,行2中对name进行赋值,所以name是局部变量,然后行1中,在未赋值前就先引用 ,所以出错

posted @ 2017-10-26 22:44  xqnq2007  阅读(133)  评论(0编辑  收藏  举报