标签:用法 内存 先后 汇总 value 默认值 -- iii 有关
形参:在定义函数阶段定义的参数称之为形式参数,简称形参,相当于变量名
def func(x, y): # x=1,y=2
print(x, y)
实参:在调用函数阶段传入的值称之为实际参数,简称实参,相当于变量值
# func(1,2)
# 形式一:
# func(1,2)
# 形式二:
# a=1
# b=2
# func(a,b)
# 形式三:
# func(int('1'),2)
# func(func1(1,2,),func2(2,3),333)
# def func(x,y):
# print(x,y)
# func(1,2,3)
# func(1,)
# func(1,2)
# func(2,1)
# def func(x,y):
# print(x,y)
# func(y=2,x=1)
# func(1,2)
# func(1,y=2)
# func(y=2,1) 错误
# func(1,y=2,x=3)
# func(1,2,x=3,y=4)
def func(x,y=3):
print(x,y)
func(x=1)
func(x=1,y=44444)
1 3
1 44444
# def func(y=2,x): 错误
# pass
# def func(x,y=2):
# pass
# 示范1:
m=2
def func(x,y=m): # y=>2的内存地址
print(x,y)
m=333333333
func(1)
1 2
# 示范2:
m = [111111, ]
def func(x, y=m): # y=>[111111, ]的内存地址
print(x, y)
#
m.append(3333333)
func(1)
1 [111111, 3333333]
def func(x,y,z,l=None):
if l is None:
l=[]
l.append(x)
l.append(y)
l.append(z)
print(l)
func(1,2,3)
func(4,5,6)
[1, 2, 3]
[4, 5, 6]
def my_sum(*args):
res=0
for item in args:
res+=item
return res
res=my_sum(1,2,3,4,)
print(res)
10
# def func(x,y,z):
# print(x,y,z)
#
# # func(*[11,22,33]) # func(11,22,33)
def func(x,y,*args): # args=(3,4,5,6)
print(x,y,args)
func(1,2,[3,4,5,6])
func(1,2,*[3,4,5,6]) # func(1,2,3,4,5,6)
func(*'hello') # func('h','e','l','l','o')
1 2 ([3, 4, 5, 6],)
1 2 (3, 4, 5, 6)
h e ('l', 'l', 'o')
# def func(x,y,**kwargs):
# print(x,y,kwargs)
#
# func(1,y=2,a=1,b=2,c=3)
1 2 {'a': 1, 'b': 2, 'c': 3}
# def func(x,y,z):
# print(x,y,z)
# func(*{'x':1,'y':2,'z':3}) # func('x','y','z')
# func(**{'x':1,'y':2,'z':3}) # func(x=1,y=2,z=3)
x y z
1 2 3
# 错误
# func(**{'x':1,'y':2,}) # func(x=1,y=2)
# func(**{'x':1,'a':2,'z':3}) # func(x=1,a=2,z=3)
# def func(x,y,**kwargs):
# print(x,y,kwargs)
# func(y=222,x=111,a=333,b=444)
# func(**{'y':222,'x':111,'a':333,'b':4444})
111 222 {'a': 333, 'b': 444}
111 222 {'a': 333, 'b': 4444}
# def func(x,*args,**kwargs):
# print(args)
# print(kwargs)
func(1,2,3,4,5,6,7,8,y=2,z=3)
(2, 3, 4, 5, 6, 7, 8)
{'y': 2, 'z': 3}
def index(x,y,z):
print('index=>>> ',x,y,z)
def wrapper(*args,**kwargs): #args=(1,) kwargs={'z':3,'y':2}
index(*args,**kwargs)
# index(*(1,),**{'z':3,'y':2})
# index(1,z=3,y=2)
wrapper(1,z=3,y=2) # 为wrapper传递的参数是给index用的
# 原格式---》汇总-----》打回原形
index=>>> 1 2 3
标签:用法 内存 先后 汇总 value 默认值 -- iii 有关
原文地址:https://www.cnblogs.com/chenyoupan/p/12520359.html