首页 > 编程语言 > 详细

python-函数2(调用)

时间:2019-12-01 18:21:52      阅读:67      评论:0      收藏:0      [点我收藏+]

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"}) 

 

 

python-函数2(调用)

原文:https://www.cnblogs.com/kezi/p/11966581.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!