Python带*参数和带**参数

简介: 置:在一个函数里只能有一个,且放在末尾。

一、带*形参


1、格式:*形参名,如*args


2、数据类型:元组


3、传参方式:接收任意个位置参数(可以不传参)。


4、位置:在一个函数里只能有一个,且放在末尾(没有带**形参的情况下)。


二、带**形参


1、格式:**形参名,如**kwargs


2、数据类型:字典


3、传参方式:接收任意个关键字参数(可以不传参)。


4、位置:在一个函数里只能有一个,且放在末尾。


def foo(n,*args,**kwargs):
    print("n=",n,"*args=",args,"**kwargs=",kwargs)
foo(10,23,45,name="tom",age=23)
n= 10 *args= (23, 45) **kwargs= {'name': 'tom', 'age': 23}


三、带*实参


1、格式:*实参名


2、意义:对序列(列表、元组、字符串)解包装


3、传参方式:不可以少传参、多传参


def foo(a,b):
    print("a=",a,"b=",b)
m=[6,9]
foo(*m)


目录
相关文章
|
1月前
|
Python
|
5天前
|
前端开发 计算机视觉
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
22 2
|
18天前
|
开发框架 JSON API
Python中FastAPI项目使用 Annotated的参数设计
Python中FastAPI项目使用 Annotated的参数设计
|
25天前
|
Python
python中位置参数和默认值
【7月更文挑战第25天】
30 7
|
25天前
|
Python
python中定义函数时使用位置参数
【7月更文挑战第25天】
28 7
|
25天前
|
Python
python中调用函数时使用位置参数
【7月更文挑战第25天】
24 1
|
26天前
|
Python
|
26天前
|
Python
Python中使用默认参数(Default Arguments)
【7月更文挑战第24天】
13 1
|
26天前
|
Python
|
26天前
|
Python
Python中使用函数参数
【7月更文挑战第23天】
17 2