形参:定义函数时括号内指定的参数
def func(a,b): #a,b形参
print(a,b)
func(1,2)#实参
实参:调用时传入的参数
位置参数
def regis(name,pwd,sex):#位置形参
print(name,pwd,sex)
regis("a",12,"man")#位置实参
默认参数(可变类型不要设置默认参数)
def func(a,b,c):
print(a,b,c)
func(a=3,b=2,c=1)
def reg(name,age,sex="women"):
print(name,age,sex)
reg("rose",18)
reg("rose",18,sex="man")
def res(name,sex,hobby):
print(name,sex,hobby)
res("app","man",["paly","music"])
可变参数
位置实参可变,形参也要可变
def fun(*args):
print(args)
fun(1,2,3,4,65,567)
带*号的形参接收任意个数的实参,接收到的实参会被打包成元组
实参带*意味着*号之后的值for循环后传给形参
形参后代**
**不接受位置实参
def fun(**kwargs):
print(kwargs)
fun(a=1,b=2,d="abv")
形参中代** 会把关键字实参打包为字典,只接受多出来的关键字实参
def fun(a,b,**kwargs):
print(a,b,kwargs)
fun(1,2,c=3)
def func(a=1,b=2,**kwargs):
print(a,b,kwargs)
func(c=1)
实参中代** 会把**后的字典类型打散成关键字实参
def fun(name):
print(name)
fun(**{"name":"tom"})
def fuv(name,pwd,**kwargs):
if name == "tom" and pwd == 123:
if "phone" in kwargs and kwargs["phone"] == 123:
print("loging successful")
else:
print("phone is none or phone is error")
else:
print("loging error")
fuv("tom",123,phone=123)
以下这种情况要位置参数在关键字参数之前
def func(*args,**kwargs):
print(args)
print(kwargs)
func(1,2,3,a=100,b=1222)