python-函数2(调用)

python-函数2(调用)

1、实参和形参调用

2、默认调用

3、参数驵调用

 

 1、实参和形参调用

def test5(x,y):                             #形参
    print(x)
    print(y)

                                             y=1
                                             x=2

test5(2,1) #实参与形参顺序依依对应,这种传参方式是位置参数。                            
test5(x=3,y=5)#这种方式是关键字传参,没有顺序。

test(3,y=2) 既有关键字又有位置参数,会按照位置参数的规则来进行。
text5(y=y,x=x)关键字与实参顺序无关
test(3,z=2,y=6)
@@@@@关键参数不能写在位置参数前@@@@@。。。

2、默认调用

def test5(x,y=5):
    print(x)
    print(y)
test5(2)
################################

def test5(x,y=5):
    print(x)
    print(y)
test5(2,y=3)

默认参数特点:调用函数的时候,默认参数非必须传递。
用途:1、默认安装值 (def test6(x,soft1=True,soft2=True:))
      2、默认数据库连接。(def test8(host,part="3306"):)

3、参数组调用

________________________

def test5(x,y=5):
    print(x)
    print(y)
test5(2)
################################

def test5(x,y=5):
    print(x)
    print(y)
test5(2,y=3)

默认参数特点:调用函数的时候,默认参数非必须传递。
用途:1、默认安装值 (def test6(x,soft1=True,soft2=True:))
      2、默认数据库连接。(def test8(host,part="3306"):)




参数组
----------------------------------------
例1:以元组方式定义

def test(*args): print(args) test(1,2,3,4,5,6) def test1(x,*args): print(args) test(*[1,2,3,4,2]) 例2以字典方式定义 def test2(**zdargs): print(zdargs)

# print(zdargs["name"])
# print(zdargs['age'])
# print(zdargs['sex'])



test2(name='kezi',age=18,sex="F")    #把N个关键字参数,转换成字典的方式
test2(**{name="kezi1",age=28,sex="M"}) 
原文地址:https://www.cnblogs.com/kezi/p/11966581.html