例1:
  1. #定义一个含有不定长参数的函数,本例第三个参数*args
  2. def sum_nums(a,b,*args):
  3. print('_'*30)
  4. print(a)
  5. print(b)
  6. print(args)
  7. #调用函数:
  8. sum_nums(11,22,33,44,55,66,77)
  9. sum_nums(11,22,33)
  10. sum_nums(11,22)
  11. sum_nums(11)#错误调用,传递的参数不够
  12. #输出结果:
  13. ______________________________
  14. 11
  15. 22
  16. (33, 44, 55, 66, 77)
  17. ______________________________
  18. 11
  19. 22
  20. (33,)
  21. ______________________________
  22. 11
  23. 22
  24. ()
  25. Traceback (most recent call last):
  26. File "6.py", line 10, in <module>
  27. sum_nums(11)#错误调用
  28. TypeError: sum_nums() missing 1 required positional argument: 'b'
例2:
  1. #定义一个含有复杂参数类型的函数,**kwargs为关键字参数
  2. def test(a,b,c=33,*args,**kwargs):
  3. print(a)
  4. print(b)
  5. print(c)
  6. print(args)
  7. print(kwargs)
  8. #函数调用
  9. test(11,22,33,44,55,task=99,done=89)
  10. #输出结果:
  11. 11
  12. 22
  13. 33
  14. (44, 55)
  15. {'task': 99, 'done': 89}
例3:拆包
  1. #定义函数
  2. def test(a,b,c=33,*args,**kwargs):
  3. print(a)
  4. print(b)
  5. print(c)
  6. print(args)
  7. print(kwargs)
  8. A=(11,22,33)
  9. B={
  10. 'task':99,'done':89
  11. }
  12. #函数调用,将A拆开后传给args,将B拆开后传给kwargs
  13. test(11,22,33,*A,**B)
  14. #输出结果
  15. 11
  16. 22
  17. 33
  18. (11, 22, 33)
  19. {'task': 99, 'done': 89}

python中函数的不定长参数的更多相关文章

  1. 学习javaScript必知必会(1)~js介绍、函数、匿名函数、自调用函数、不定长参数

    一.简单了解一下JavaScript(js) 1.什么是js? js:是网景公司开发的,是基于客户端浏览器, 面向(基于)对象.事件驱动式的页面脚本语言. 2.什么场景下使用到js? 表单验证.页面特 ...

  2. python——装饰器(不定长参数,闭包,装饰器)示例

    def func(functionName): print("正在装饰") def func_in(*args, **kargs): print("------func_ ...

  3. Python装饰器AOP 不定长参数 鸭子类型 重载(三)

    1 可变长参数与关键字参数 *args代表任意长度可变参数 **kwargs代表关键字参数 用*args和**kwargs只是为了方便并没有强制使用它们. 缺省参数即是调用该函数时,缺省参数的值若未被 ...

  4. python不定长参数 *argc,**kargcs(19)

    在 python函数的声明和调用 中我们简单的了解了函数的相关使用,然而在函数传递参数的时候,我们埋下了一个坑,关于不定长参数的传递我们还没有讲,今天这篇文章主要就是讲解这个问题. 一.函数不定长参数 ...

  5. python函数中的不定长参数

    python自定义函数中有两中不定长参数,第一种是*name,第二种是**name.加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数.加了两个星号 ** 的参数会以字典的 ...

  6. python函数中的参数(关键字参数,默认参数,位置参数,不定长参数)

    默认参数:定义函数的时候给定变量一个默认值. def num(age=1): 位置参数:调用函数的时候根据定义函数时的形参位置和实参位置进行引用. 关键字参数:如果定义的函数中含有关键字参数,调用函数 ...

  7. 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)

    详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类   Python中函数参数定义及调用函数时传参 ...

  8. Python中函数的参数传递与可变长参数

    转自旭东的博客原文 Python中函数的参数传递与可变长参数 Python中传递参数有以下几种类型: (1)像C++一样的默认缺省函数 (2)根据参数名传参数 (3)可变长度参数 示例如下: (1)默 ...

  9. python中函数的参数解析

    python中函数的各种参数梳理: 1.形参:函数定义时传入的参数 2.实参:函数调用时传入的参数 (有形参必传实参,形参里自身特点可不传的,可传可不传) 3.缺省参数:不传为默认值,传了会覆盖(下面 ...

随机推荐

  1. Python 连接Sql Server数据库 MSSql

    Python 想要和MSSql数据库进行交互,首先要下载名为"pymssql"的包,然后import该包即可. 地址:https://pypi.python.org/pypi/py ...

  2. (一)Hybrid app混合开发模式

    hybrid app是什么? 这里我们先看一下词条上的定义 Hybrid App:Hybrid App is a mobile application that is coded in both br ...

  3. JQ单双引号转义

    var temp = "${row.address_province}"; alert(temp);——————即变量temp alert("\'"+temp+ ...

  4. Kindeditor单独调用多图上传

    html代码: <input type="button" id="J_selectImage" value="图片上传" />& ...

  5. Android 极光推送JPush---自定义提示音

    极光推送提供三种方法实现Notification通知 三方开发平台发送普通消息,客户端设置PushNotificationBuilder,实现基础的Notification通知 三方开放平台发送普通消 ...

  6. Struts2_Struts标签

    1.property <s:property value="username" /> 一般访问,访问ValueStack中的第一个username <s:prop ...

  7. centos6.5_64bit-Tomcat7安装部署

    此次安装系统版本及软件版本 centos6.5-64bit java -1.7.0_45 jdk1.8.0_111 apache-tomcat-7.0.73   一.检查java版本信息        ...

  8. **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  9. 用dynamic的方式来转换Json对象

    来自这里:http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object If you ...

  10. EF Database first 中,实现 多个表对应一个 实体的 查询

    1.首先 创建好 数据 库. hobby表 major 表 student 表 外键 关系如下 2. 实现将 数据库 映射到EDM中 视图如下 在VS中 生成了 3个实体类  ,对应的是 数据库中的3 ...