python中的*args与**kwargs的含义与作用
Python 作用 含义 args kwargs
2023-06-13 09:11:25 时间
一、*args与**kwargs介绍
*args是非关键字参数,用于元组,**kwargs是关键字参数 (字典)
也就是说args表示任何多个无名参数,然而kwags表示一个一个有着对应关系的关键字参数,
在使用的时候需要注意,*args要在**kwags之前,不然会发生语法错误
二、args使用案例和kwargs使用案例
# coding=utf-8
"""
@project: panglu_test_59
@Author:gaojs
@file: test017.py
@date:2022/7/13 14:00
@blogs: https://www.gaojs.com.cn
"""
def arg_test(a, b, c, d, arg):
"""
arg练习
"""
print(a, b, c, d, arg)
def args_test(a, b, *args):
"""
*args练习
"""
print(a, b, args)
print(type(args))
def kwargs_test(**kwargs):
"""
*args练习
"""
print(kwargs, type(kwargs))
def args_kwargs_test(arg, arg1, *args, **kwargs):
"""
*args练习
"""
print(arg, arg1, args, kwargs)
if __name__ == '__main__':
# arg练习
arg_test(1, 2, 3, 4, 444)
# args练习
args_test(6, 5, 55, 798, 5456)
# kwargs练习:结果是字典
kwargs_test(a=1, b=2, c=3, d=4)
# 混合练习
args_kwargs_test(1, 2, 3, 4, 5, 6, a=7, b=8, c=9)
输出结果:
E:\panglu_test_59\venv\Scripts\python.exe E:/panglu_test_59/test017.py
1 2 3 4 444
6 5 (55, 798, 5456)
<class 'tuple'>
{'a': 1, 'b': 2, 'c': 3, 'd': 4} <class 'dict'>
1 2 (3, 4, 5, 6) {'a': 7, 'b': 8, 'c': 9}
Process finished with exit code 0
相关文章
- Python面试基础知识_python自学需要哪些基础知识
- Python turtle 模块可以编写游戏,是真的吗?
- python读写、创建文件、文件夹等等
- python 去掉文件后缀名,python 删除后缀名文件
- python环境搭建和pycharm的安装配置及汉化(零基础小白版)[通俗易懂]
- 20210225-1 Python错误与异常「建议收藏」
- 【说站】python创建可变变量的方法
- 经纬度距离计算 python_Python已知两坐标求距离
- python zipfile_Python 学习入门(16)—— zipfile
- python判断文件后缀_Python 判断文件后缀是否被篡改
- random生成随机整数 python_python中的random函数
- Python在接口测试中的应用
- python定义函数求和_Python定义函数实现累计求和操作
- Python 常用外部模块详解
- Python注释是什么?Python注释的含义作用种类及使用方法
- python – 文件
- scrapy 管理部署的爬虫项目的python类详解程序员
- python实现的Caesar加解密算法详解编程语言
- python检测端口是否开放详解编程语言
- python读取ini配置文件详解编程语言
- Python学习:6.python内置函数详解编程语言
- 如何使用Python安装MySQL(python安装mysql)
- python使用Python轻松操作Redis(redis-)
- python计算最大优先级队列实例
- Python群发邮件实例代码