blog/docs/code/python/kebiancanshu.md

1.2 KiB
Raw Blame History

title date tags categories author
函数 - 可变参数 2020-09-13 17:25:10
Python
Python
Anges黎梦

" *paramter "

表示接收任意多个实际参数,并将其放到一个元组中。

  • 传递任意数量的参数

示例代码

def func_1(*items):
    print(items)
    print(type(items))

# 传递任意数量的参数
func_1(1,2,3,45,"ffff")
  • 以列表形式传递参数

响应结果

(1, 2, 3, 45, 'ffff')
<class 'tuple'>

示例代码

def func_1
list_1 = [1, 2, 3, 45, 'ffff','aaaa']

以列表形式传递参数
func_1(*list_1)

响应结果

(1, 2, 3, 45, 'ffff', 'aaaa')
<class 'tuple'>

" **paramter "

表示接收任意多个实际参数,并将其放到一个字典中。

  • 直接传入键对值调用

示例代码

def func_2(**params):
    print(params)
    print(type(params))
func_2(a=1, b=6,c=9)

响应结果

{'a': 1, 'b': 6, 'c': 9}
<class 'dict'>
  • 通过字典传输可变参数

示例代码

dict_1 = {"a":1,"b":2,"c":3}
func_2(**dict_1)    # 通过字典,对可变参数传值

响应结果

{'a': 1, 'b': 2, 'c': 3}
<class 'dict'>