本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – 不定长函数参数列表
原文链接:https://www.stubbornhuang.com/1251/
发布于:2021年04月08日 22:53:12
修改于:2021年04月08日 22:53:40

python中实现函数不定长函数参数列表一般采用星号*,而星号又有两种方式:
- 单星号*一般在不指定参数时使用,而参数会以元祖tuple的形式传入,且各个参数会维持原有参数类型
- 双星号**一般用于全部以指定参数传入的情况,而参数会以字典dit的形式传入,且各个参数维持原有参数类型
1 单星号
代码示例:
def call_tuple(*args):
print('不定长可变参数:元祖')
for x in args:
print(x, type(x))
if __name__ == '__main__':
call_tuple(1,2,3,4,'ssss')
输出结果:
不定长可变参数:元祖
1 <class 'int'>
2 <class 'int'>
3 <class 'int'>
4 <class 'int'>
ssss <class 'str'>
2 双星号
代码示例:
def call_dit(**args):
print('不定长可变参数:字典')
for value in args.values():
print(value, type(value))
if __name__ == '__main__':
call_dit(name='lihao',age=18,sex='男')
输出结果:
不定长可变参数:字典
lihao <class 'str'>
18 <class 'int'>
男 <class 'str'>
当前分类随机文章推荐
- Python - ModuleNotFoundError: No module named 'absl' 阅读222次,点赞0次
- Python3爬虫 - requests库 阅读2844次,点赞3次
- Python:UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 12305,以及中文乱码的解决方案 阅读2290次,点赞0次
- Python3爬虫 - 下载反盗链图片的方式 阅读2067次,点赞1次
- Python - 普通函数/lambda匿名函数/类成员函数作为回调函数的用法 阅读1301次,点赞0次
- Python - 使用python-opencv裁剪原视频为与视频高同宽的视频 阅读801次,点赞0次
- Python - 使用Opencv-Python库获取本机摄像头视频并保存为视频文件 阅读1587次,点赞0次
- Pip - 常用命令(安装,卸载,升级第三方库) 阅读2382次,点赞1次
- Python - 使用with open as 读写文件 阅读836次,点赞0次
- Python - 各种包安装、导入问题总结 阅读809次,点赞0次
全站随机文章推荐
- Pytorch - 检测CUDA、cuDNN以及GPU版本的Pytorch是否安装成功、GPU显存测试 阅读1270次,点赞1次
- WordPress - 下载安装插件失败,无法创建目录 阅读3473次,点赞0次
- 资源分享 - Computer Animation - Algorithms and Techniques (Third Edition) 英文高清PDF下载 阅读1158次,点赞0次
- 资源分享 - Introduction to Computer Graphics - A Practical Learning Approach 英文高清PDF下载 阅读312次,点赞0次
- 资源分享 – Fluid Simulation for Computer Graphics, Second Edition英文高清PDF下载 阅读2456次,点赞0次
- 资源分享 - Data Structures and Algorithms for Game Developers 英文高清PDF下载 阅读549次,点赞0次
- 资源分享 - Digital Lighting & Rendering , Third Edition 英文高清PDF下载 阅读307次,点赞0次
- C++ - 最简单的将文本文件的内容一次性读取到std::string的方法 阅读2431次,点赞2次
- 资源分享 - Computer Graphics and Geometric Modelling - Implementation and Algorithms 英文高清PDF下载 阅读201次,点赞0次
- 三维重建 - 基于RBF的三维网格重建 阅读3253次,点赞4次
评论
144