6. The dynamic parameter function
* Args, ** kwargs: accept the dynamic positional parameters and dynamic parameters of keywords
*args -- tuple
*kwargs -- dict
Dynamic Parameters priority: location parameter> dynamic positional parameters> default parameters (key parameter)> Dynamic keyword arguments
1.*.args:
Receiving dynamic parameters of the position, accept any number, defined as * phase polymerization stage call is broken *
def func(a,b,*args):#*.args可以替换,*s
print(a,b,args)
print(a,b,*args) #*为打散
func(1,2,3,4,5,6)
#优先级: 位置参数 > 动态参数
def func(a=1,b=2,*args):#不推荐使用,不会报错
print(a,b,args)
func(1,2,3,4,5,6)
#默认参数放在动态参数后面
def func(*args,a=11,b=12):
print(a,b,args)
func(1,2,3,4,5,6)
#*args:元组,接收位置参数
2. ** kwargs:
Receiving dynamic keyword arguments
#**kwargs:字典,接收关键字参数。
def func(a,b,**kwargs):
print(a,b,kwargs)
func(1,2,3,4,5,6)
def func(a,b,*args,c=1,d=2,**kwargs):
print(a,b,args,c,d,kwargs)#*kwargs:获取是字典的键
print(*kwargs)
func(1,2,3,4,c=8,d=10,e='alex',f='wusir')
. 3 # * args, ** kwargs combination:
def func(*args,**kwargs):
print(args,kwargs)
func(1,2,3,4,a=5,b=2,c=7)
4. Extension: pass in the dictionary list and method
#列表
list1 = [12,23,3,4,5,6]
def func(*args):
print(*args)
func(list1[0],list1[1]...)#笨方法
func(*list1)#用*打散全部上传
#字典
dict1 = {'k1':1,'k2':2}
def func(**kwargs):
print(kwargs)
func(**dict1)
7. Notes function:
reflection
def func(user,passwd):
"""
密码加密
:param user: 用户名 str
:param passwd: 密码 str
:return: 加密的密码
"""
return user,passwd
print(func.__doc__)#查看函数注释
print(func.__name__)#查看函数名
8. namespace:
Global space: The current py need to open up space to store files in the global space (no indentation)
Local space: the function of the open space is local space
Built-in space: built-in functions for storage
# Every time you run, it will load the built space and global space
Loading order: inner space> Global Space> local space
Value order: local spaces> Global Space> inner space
Scope:
Global scope: the built-in global space + space
The local scope: local space
10. The nesting function
1. Simple Nested
def func():
print(1)
def f1():
print(2)
return f1()#f1没有返回值,默认返回None
func()#结果: 1 \n 2
#不管在什么位置,只要是函数名+(),就是调用此函数
def func():
a = 1
def foo():
b = 2
print(b)
print(a)
def f1():
print(b)
return f1()
return foo #返回的是foo的内存地址,没有(),表示没有调用foo函数
print(func())#结果:<function func.<locals>.foo at 0x000002C49CBADD90>
#**调用foo.
def func():
def foo():
a = 1
print(a)
return doo
#a = func(),得到foo的内存,然后a()调用foo
a = func()
a()
2. function refer to each other:
def func():
a = 1
foo()
print(a)
def foo():
b = 2
print(b)
func()#结果: 2 \n 1
foo()#结果:2
Similar functions: data do not share the same level of local space
def a():
a = 1
c()
print(c)
def b():
b = 2
print(b)
def c():
c = 3
print(a)
def run():
a()
run()#结果
#<function a at 0x0000015464512F28>
#<function c at 0x00000154661DDC80>
#同级局部空间的数据不共享,输出的是函数名对应的内存地址
3. The parameters passed nested functions:
def func(a):
foo(a)
def foo(e):
b(e)
def b(c):
print(c)
func(15)#结果:15
def func(a):
foo(a)
def foo(e):
b(e)
def b(c):
print(c)
return 10
print(func(15))#结果:15 \n None
#每一个函数都需要返回上一层结果才能返回最深层结果
def func(a):
return(foo(a))
def foo(e):
return(b(e))
def b(c):
print(c)
return 10
print(func(15))#结果:15 \n 10
11.global,nonlocal
1.global:
Declare global variables, global variables can be modified
a = 10
def func():
#a += 1#局部不可修改全局数据
global a#声明全局变量,可以修改
a +=1
print(a)
func()
print(a)
2.nonlocal:
Nonlocal variables declared a local variable is not, nor is it a global variable, but variable in the external nested functions
The function of the outer layer, modify variable values in the local space, the variables do not involve the global space, only the most recent modification of the upper variable function, a layer not continue to look up until the outermost function, if most there is no outer layer, on the error.
a = 10
def func():
def f1():
a = 12
def fool():
nonlocal a
a += 1
print(a) #13
print(a)# 12
fool()
print(a)#13
print(a)# 10
f1()
print(a)#10
func()
print(a) #10