函数调用时的参数传递方式:(调用函数) 传递方式有以下四种: 位置传参 序列传参 关键字传参 字典关键字传参
---------------------------------------------------yu---

(1)位置传参:
实际参数(实参)的对应关系与形式参数(形参)对应关系是按位置依次对应的
实参的个数和形参的个数要严格一直
实例:

def myfun1(a, b, c):   # 形参            print(a)            print(b)            print(c)      # 调用      myfun1(1, 2, 3)   # 实参

(2)序列传参
序列传参是指在函数调用过程中,用*将序列拆解后按位置进行传递的方式
序列传参时,序列拆解的位置将与形参一一对应
序列的位置信息对应相应的参数
实例:

# 假设已经有序列可用      L1 = [11,22,33]      T1 = (4.4,5.5,6.6)      S1 = "ABC"      myfun1(*L1)      myfun1(*T1)      myfun1(*S1)

(3)关键字传参:
关键字传参是指传参时,按形参的名称给形参赋值
实参和形参按形参名进行匹配(可以不按位置顺序进行匹配)
实例:

# 关键字传参      myfun1(c = 33, b = 22, a = 11)

(4)字典关键字传参:
是指实参为字典,将字典用**拆解后进行关键字传参
实例:

d1 = {"c":33, "b":22, "a":11}      myfun1(**d1)

说明:字典的键名和形参名必须一
字典的键名必须为字符串,并符合关键字的命名
字典的键名要在形参中存在

(5)函数的综合
传参方式,在能确定每个形参否能唯一匹配一个实参的情况也可以任意传参
传参时,位置传参和序列传参要先于关键字传参和字典关键子传参
实例: myfun1(100, *[200, 300])

         myfun1(*[10,20],30)         myfun1(*[10], 20, *[30])         myfun1(100, **{"c":300, "b":200})         myfun1(**{"c":300, "b":200}, a = 100)         myfun1(b = 200, c = 300, 100)   # 错的

注意传参时:
1、不可变类型的数据作为函数参数传入时,函数内部不会改变变量的原数据的值
2、可变类型的数据作为函数参数传递时,函数内部可以改变原数据,多用与返回更多函数执行结果
实例:写一个函数,在函数内输入一些整数,

  (1)把所有奇数放入全局列表odds中  (2)把所有的偶数放入全局的列表evens中odds = []evens = []def input_number(o, e):    while True:        n = int(input("请输入整数:"))        if n < 0:            break        # 如果n为奇数则添加列表odds中        if n % 2 == 1:            o.append(n)        else: # 如果n为偶数则添加到列表evens中            e.append(n)input_number(odds, evens)print(odds)   # 此列表将被修改print(evens)    # 此列表也将被修改

2、函数参数的定义规则(创建函数)
函数的缺省参数:
语法:def 函数名(形参名1 = 默认实参1, 形参名2 = 默认实参2, ....):

语句块

实例:以下示意函数的缺省参数
def info(name, age=1, address="未填写"):

print(name, "住在:", address, "今年:", age, "岁")

info("庄AC", 23, "北京")
info("庄AC", 23)
说明:(1)缺省参数必须自右至左依次存在,如果一个参数有缺省参数,则其右侧的所有参数都必须有缺省参数

            如:def test_fn(a, b = 10, c):    # 是错的     (2)缺省参数可以有0个或多个,甚至全部都有缺省参数

3、函数的参数定义方式:

 位置形参 星号元组形参 命名关键字形参 双星号字典形参

(1)位置形参语法:
def 函数名(形参名1, 形参名2, 形参名3,......):

语句块

(2)星号元组形参语法:
def 函数名(*元组形参名):

语句块

作用:收集多余的位置传参
实例:

def funs(*args):       # args绑定一个元组,此元组内的元素是为了剩余的位置传参       print("施参个数是:", len(args))       print("args绑定", args)   funs()  # 以无参调用   funs(1, 2, 3, 4)   funs("ABC", 1, 2.3, 4+5J)

(3)命名关键字形参语法:
def 函数名(*, 命名关键字形参):
语句块

def 函数名(*args, 命名关键字形参):
语句块
作用:所有的命名关键字形参,必须用关键字传参或字典关键字传参进行参数传递
实例:

 def fn(*, a, b):       print("a的值为:", a)       print("b的值为:", b)   # fn(1, 2)  # 错的,a和b 必须是用关键字传参   fn(a = 1, b = 2)  #必须这么传   fn(b = 0, a = 20)   fn(**{"b":200, "a":100})

 def f2(*args, a, b):       print("args=", args)       print("a=", a)       print("b=", b)   f2(1, 2, 3, 4, a=100, b=200)   f2(a=100, b=200)

(4)双星号字典形参语法:
def 函数名(**字典形参名):
语句块

作用:收集多余的关键字传参(例如:dict()),通常只有一个
实例:

   def func(**kwagrs):  # kwagrs绑定一个字典       print("参数个数:", len(kwagrs))       print("kwagrs的值为:", kwagrs)   func(name="zhuang", age=35, address="dgas")   func()

(5)函数参数自左至右的顺序为:
位置形参, 星号元组形参, 命名关键字形参, 双星号字典形参
实例:

def fn(a, b, *args, c, **kwargs):      passfn(100,200,300,400,c=5.5,d=6.6,e=7.7)fn(10,*"AB",20,*{"a":100, "b":200, "c":300})

# ----------以下函数可以接受任意的位置传参和关键字传参------------
def fn(args, *kwargs):





额外部分一:

python函数参数支持带星号*的非固定个数参数,如:

def getInfos(msg,*users):    for u in users:        print(u)

在调用时,可以往users传入一个元组或列表,如果传入的列表或元组前带星号,刚传入后会自动拆包。具体的不同用法有不同的效果,如下:

1、不定形参传入元组。

getInfos('msg',('aa','bb') )

输出:

('aa', 'bb')

2、不定形参传入带星元组。

getInfos('msg',*('aa','bb') )

输出:

aabb

3、不定形参传入列表。

getInfos('msg',['aa','bb'] )

输出:

['aa', 'bb']


光看结果可能像是列表!!!!!!!!!!!!!但其实返回的就是一个元组
可通过以下代码进行验证:

#!/usr/bin/python3def getInfos(msg,*users):    users[0]="hh"    for u in users:        print(u)a = ['aa','bb']getInfos('msg',a )

结果:

Traceback (most recent call last):  File "/tmp/653879115/main.py", line 7, in     getInfos('msg',a )  File "/tmp/653879115/main.py", line 3, in getInfos    users[0]="hh"TypeError: 'tuple' object does not support item assignmentexit status 1

这表明是一个元组。

4、不定形参传入带星列表。

getInfos('msg',*['aa','bb'] )

输出 :

aabb

5、直接传入多个值。

getInfos('msg', 'aa','bb')

输出:

 aa

额外部分二:

看如下例子:

def singalStar(common, *rest):    print("Common args: ", common)    print("Rest args: ", rest) 

第一种方式,星号(*)参数不传参:

 singalStar("hello")         #Common args:  hello    #Rest args:  ()

第二种方式,传多个值(个数大于或等于函数定义时的参数个数):

  singalStar("hello", "world", 000)         #Common args:  hello    #Rest args:  ('world', 0)

不难看出,上述方式中,星号参数把接收的参数合并为一个元组。

第三种方式,竟然星号参数把接收的参数作为元组,那么我们直接传元组类型的值:

 singalStar("hello", ("world", 000))         #Common args:  hello    #Rest args:  (('world', 0),)

没错,你没看错,传递的元组值作为了星号参数的元组中的一个元素。

第四种方式,但是有时候我们想把元组值就作为星号参数的参数值,那么该怎么办呢?好办,在元组值前加上“”即可,不过此时,就不能在加了“”的元组后,追加任何值了。

singalStar("hello", *("world", 000))

singalStar("hello", *("world", 000), "123") #error

Common args: hello

Rest args: ('world', 0)

3.带两个星号(*)的函数参数

带两个星号(*)的函数定义如下:

   def doubleStar(common, **double):        print("Common args: ", common)        print("Double args: ", double)

第一种方式,星号(*)参数不传值:

doubleStar("hello")

Common args: hello

Double args: {}

第二种方式,传多个参数(个数大于或等于函数定义时的参数个数)。但是,这和单星号参数传值方式肯定不一样,否则,不就乱套了吗。

doubleStar("hello", "Test", 24) #error
doubleStar("hello", x = "Test", y = 24)

Common args: hello

Double args: {'y': 24, 'x': 'Test'}

不难发现,此时必须采用第一节的默认值传参的“args = value”的方式。同时,“=”前的字段成了字典的键,“=”后的字段成了字典的值。即,双星号参数接收的参数作为字典。

第三种方式,有时候我们想把字典值就作为星号参数的参数值,那么该怎么办呢?同单星号参数,在字典值前加上“**”,同时其后不能添加任何值。

#doubleStar("hello", **{"name": "Test", "age": 24}, {"name": "Test2", "age": 24})    #error#doubleStar("hello", {"name": "Test", "age": 24})    #errordoubleStar("hello", **{"name": "Test", "age": 24}) #Common args:  hello#Double args:  {'name': 'Test', 'age': 24}

在有些情况下,单星号函数参数和双星号函数参数是一起使用的,定义如下:

 def singalAndDoubleStar(common, *single, **double):        print("Common args: ", common)        print("Single args: ", single)        print("Double args: ", double)

4.总结

默认值函数参数。这种函数定义时,第一个有默认值的参数后的每一个参数都必须提供默认值。传参时,可以直接传参,也可以以“默认值参数名=value”的形式传参。单星号函数参数。单星号函数参数接收的参数组成一个元组。双星号函数参数。双星号函数参数接收的参数组成一个字典。

完整的代码如下:

def singalStar(common, *rest):    print("Common args: ", common)    print("Rest args: ", rest)    def doubleStar(common, **double):    print("Common args: ", common)    print("Double args: ", double)    def singalAndDoubleStar(common, *single, **double):    print("Common args: ", common)    print("Single args: ", single)    print("Double args: ", double) def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):    print("Common args", common)    print("Default String", defaultStr)    print("Default Number", defaultNum)    if __name__ == "__main__":    defaultValueArgs("Test")    defaultValueArgs("Test", "default", defaultNum = 1)         singalStar("hello")    singalStar("hello", "world", 000)    singalStar("hello", ("world", 000))    singalStar("hello", ("world", 000), {123: 123})    singalStar("hello", *("world", 000))#     singalStar("hello", *("world", 000), "123")    #error        doubleStar("hello")    doubleStar("hello", x = "Test", y = 24)    doubleStar("hello", **{"name": "Test", "age": 24})#     doubleStar("hello", {"name": "Test", "age": 24})    #error     singalAndDoubleStar("hello")    singalAndDoubleStar("hello", "world", 000)    singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})    singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})    singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})#     singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24})      #error    singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})