1-1 def test4(name,age=9999,*args,**kwargs): print(name) print(age) print(args) print(kwargs) logger("TEST4") def logger(source): print("from %s" % source) test4(‘itachi‘,age=21,sex=‘m‘,hobby=‘sweet_rice_ball‘) ---> itachi 21 () {‘sex‘: ‘m‘, ‘hobby‘: ‘sweet_rice_ball‘} from TEST4 运行结果没有问题
1-2 局部变量 def change_name(name): print("before change",name) name = "Uchiha Itachi" print("after change",name) name = ‘itachi‘ change_name(name) ---> before change itachi after change Uchiha Itachi # 局部变量,只在函数里生效;这个函数就是这个变量的作用域
1-3 school = ‘zxcv edu‘ def change_name(name): school = ‘Mage Linux‘ print(‘before change‘,name,school) name = ‘Uchiha Itachi‘ age = 23 print(‘after change‘,name) print(school) name =‘itachi‘ change_name(name) print(name) ---> zxcv edu before change itachi Mage Linux after change Uchiha Itachi itachi # 全局变量,在函数中是改不了的,函数作用域只在小函数中生效 # 虽然叫同样的名字,但是没关系 # 函数中默认,局部变量无法修改全局变量
1-4 如果想在局部变量中修改全局变量,应该怎么办? 声明 global school = ‘zxcv edu‘ def change_name(name): global school # 在函数中修改全局变量 school = ‘Mage Linux‘ print(‘before change‘,name,school) name = ‘Uchiha Itachi‘ age = 23 print(‘after change‘,name) name =‘itachi‘ change_name(name) print(name) print(‘school:‘,school) ---> before change itachi Mage Linux after change Uchiha Itachi itachi school: Mage Linux # 这种情况是可以修改的
原文:https://www.cnblogs.com/azxsdcv/p/14075058.html