Python不定参数函数
1. 元组形式
def test1(*args):
print('################test1################')
print(type(args))
print(args)
正确调用:
test1(1, 2) #args在函数体内部为tuple类型
错误调用:
test1(1, b=2) #TypeError: test1() got an unexpected keyword argument 'b'
test1(a=1, b=2) #TypeError: test1() got an unexpected keyword argument 'a'
test1(a=1, 2) #TypeError: test1() got an unexpected keyword argument 'a'
2. 字典形式
def test2(**kargs):
print('################test2################')
print(type(kargs))
print(kargs)
正确调用:
test2(a=1, b=2) #kargs在函数体内部为dict类型
错误调用:
test2(1, 2) #TypeError: test2() takes exactly 0 arguments (2 given)
test2(1, b=2) #TypeError: test2() takes exactly 0 arguments (2 given)
test2(a=1, 2) #SyntaxError: non-keyword arg after keyword arg
3. 混合形式
def test3(*args, **kargs):
print('################test3################')
print(type(args))
print(args)
print(type(kargs))
print(kargs)
正确调用:
test3(1, 2) #args在函数体内部为tuple类型,kargs为空dict类型
test3(1, b=2) #args在函数体内部为tuple类型,kargs为dict类型
test3(a=1, b=2) #args在函数体内部为空tuple类型,kargs为dict类型
错误调用:
test3(a=1, 2) #SyntaxError: non-keyword arg after keyword arg
4. 其他形式1
def test4(a = ()):
print('################test4################')
print(type(a))
print(a)
正确调用:
test4((1, 2)) #a在函数体内部为tuple类型
test4(a=(1, 2)) #a在函数体内部为tuple类型
test4((1,)) #a在函数体内部为tuple类型
test4(a=(1,)) #a在函数体内部为tuple类型
test4((1)) #a在函数体内部为int类型,非tuple类型
test4(a=(1)) #a在函数体内部为int类型,非tuple类型
test4(1) #a在函数体内部为int类型,非tuple类型
test4(a=1) #a在函数体内部为int类型,非tuple类型
错误调用:
test4(1, 2) #TypeError: test4() takes at most 1 argument (2 given)
test4(1, b=2) #TypeError: test4() got an unexpected keyword argument 'b'
test4(a=1, b=2) #TypeError: test4() got an unexpected keyword argument 'b'
5. 其他形式2
def test5(b = {}):
print('################test5################')
print(type(b))
print(b)
正确调用:
test5({'a':2}) #b在函数体内部为dict类型
test5(b={'a':2})
test5({'a':2,'b':3})#b在函数体内部为dict类型
test5(b={'a':2,'b':3})
test5(b=2) #b在函数体内部为int类型,非dict类型
错误调用:
test5(a=1, b=2) #TypeError: test5() got an unexpected keyword argument 'a'
test5(1, 2) #TypeError: test5() takes at most 1 argument (2 given)
test5(1, b=2) #TypeError: test5() got multiple values for keyword argument 'b'
6. 其他形式3
def test6(a = (), b = {}):
print('################test6################')
print(type(a))
print(a)
print(type(b))
print(b)
正确调用:
test6(1, 2)
test6(a=1, b=2)
test6(a=1, b=2)
test6((1, 2), {'c':8})
test6({'c':8})
test6(b={'c':8})
test6((1, 2), b=2)
test6((1, 2), b=2)
错误调用:
test6(a=1, 2) #SyntaxError: non-keyword arg after keyword arg
test6(1, 2, b=2) #TypeError: test6() got multiple values for keyword argument 'b'
关于不定参数函数中使用传入参数调用其他固定参数函数的使用请移驾至:http://www.cnblogs.com/doudongchun/p/3704123.html
Python不定参数函数的更多相关文章
- Python 不定参数函数
1. 元组形式 def test1(*args): print('################test1################') print(type(args)) print(arg ...
- 不定参数函数原理以及实现一个属于自己的printf函数
一.不定参数函数原理 二.实现一个属于自己的printf函数 参考博文:王爽汇编语言综合研究-函数如何接收不定数量的参数
- c++不定参数函数
不定参数当年做为C/C++语言一个特长被很多人推崇,但是实际上这种技术并没有应用很多.除了格式化输出之外,我实在没看到多少应用.主要原因是这种技术比较麻烦,副作用也比较多,而一般情况下重载函数也足以替 ...
- Python可变参数函数用法详解
来自:http://c.biancheng.net/view/2257.html 很多编程语言都允许定义个数可变的参数,这样可以在调用函数时传入任意多个参数.Python 当然也不例外,Python ...
- python 可变参数函数定义* args和**kwargs的用法
python函数可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法.其中,*args是可变的positional arguments列表,**kwargs是 ...
- GO语言练习:不定参数函数
1.代码 2.运行 1.代码 package main import "fmt" func MyPrintf(args ...interface{}){ for _, arg := ...
- PYTHON不定参数与__DOC__
def total(initial = 5, *numbers, **keywords): count = initial for number in numbers: count += number ...
- oc自定义不定参数函数
-(void)getValueFormConfig:(NSString *)key,... or -(void)getValueFormConfig:(NSString *)key,...NS_REQ ...
- UE3多参数函数实现
基础宏定义 #define VARARG_EXTRA(A) A, #define VARARG_NONE #define VARARG_PURE =0 static inline DWORD Chec ...
随机推荐
- 红黑树与AVL树
概述:本文从排序二叉树作为引子,讲解了红黑树,最后把红黑树和AVL树做了一个比较全面的对比. 1 排序二叉树 排序二叉树是一种特殊结构的二叉树,可以非常方便地对树中所有节点进行排序和检索. 排序二叉树 ...
- 现代C++之理解auto类型推断
理解auto类型推断 上一篇帖子中讲述了模板类型推断,我们知道auto的实现原理是基于模板类型推断的,回顾一下模板类型推断: template <typename T> void f(Pa ...
- zabbix系列(八)zabbix添加对web页面url的状态监控
通过zabbi做web监控不仅仅可以监控到站点的响应时间,还可以根据站点返回的状态码,或者响应时间做报警 1.对需要监控的主机添加web监控 在configuration—hosts 中打开主机列 ...
- CentOS 6.5下的lamp环境rsyslog+MySQL+loganalyzer实现日志集中分析管理
前言 rsyslog系统日志,在CentOS5上叫syslog,而在CentOS6上叫rsyslog,是增强版的syslog,CentOS5上的配置文件在/etc/syslog.conf下,而Cent ...
- Ex 6_16 旧货销售问题_第七次作业
即可 子问题定义:定义数组B(S,j),其中 B(S,j)表示在子集S中结束位置为j的子问题的最大收益值,其中j的前一个地点有两种情况,第一种情况是某个拍卖会 另一种情况是从家里出发. 递归关系: 初 ...
- 解析神奇的 Object.defineProperty
这个方法了不起啊..vue.js是通过它实现双向绑定的..而且Object.observe也被草案发起人撤回了..所以defineProperty更有必要了解一下了. 几行代码看他怎么用 var a= ...
- redhat5 设置静态ip
Last login: Sat Oct 14 16:19:13 2017 # 进入ip凭证文件设置地方 [root@oracle ~]# cd /etc/sysconfig/network-scrip ...
- java多线程快速入门(十)
synchonizd解决安全性问题 package com.cppdy; class MyThread6 implements Runnable{ private Integer ticketCoun ...
- Python 时间获取
摘自:http://www.jb51.net/article/91365.htm 摘自:https://www.cnblogs.com/liuq/p/6211005.html 一.在python中,除 ...
- PHP函数比较变量
作为一个PHPer,一定要多看PHP手册,每过一遍手册,你总会发现一些以前被你忽略的一些东西:我们可以从各个不同的地方/渠道学习到PHP的相关知识,但也不要忘记我们的手册,PHP手册永远是学习和提升P ...