首页 > IT职业 > IT职场 > Python函数嵌套变量的使用方法
很多小伙伴学习了Python函数嵌套,但是不会使用变量,今天小编就来带大家一起学习下吧。
主要方法如下:
1.*args 把所有位置参数聚合到元组中
2.**kwargs 把所有的关键字参数聚合到字典中
3.‘*’在函数的调用时,代表打散
4.默认参数的陷阱!
def func(name,alist=[]): alist.append(name) return alist res = func('alex') res2 = func('panda') #res == ['alex'] #res2 == ['alex','panda '] #如果 默认参数 指向一个可迭代的数据类型
5.局部嵌套定义的函数需要用 nonlocal 声明
def wrapper(): count = 1 def inner(): nonlocal count count += 1
以上就是Python函数嵌套使用变量的方法。更多Python学习推荐:PyThon学习网教学中心。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2548/viewspace-2832433/ ,如需转载,请注明出处,否则将追究法律责任。