如何在函数中修改局部变量
#!\usr\bin\env\python
# -*- coding:utf -8 -*9
school ="0ldboy edu." #定义全局变量
def change_name(name):
global school #利用global修改全局变量
school = "Mage Linux"
print("before change",name,school)
name = "Alex Li" #这个函数就是这个变量的作用域,只在这个函数内部生效
print("after change ",name)
print("school:",school) #打印之前的全局变量
name= "alex"
change_name(name) #调用change_name(name)
print(name)
print("school:",school) #打印查看修改后的全局变量
输出结果:
school :"0ldboy edu."
before change:Mage Linux
after change:Alex Li
alex
school :Mage Linux