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 ...
随机推荐
- eclipse删除多余的工作空间
window->preferences->startup and shutdown->workspaces如图:
- SYN flooding引发的网络故障
故障现象: 1.应用无法通过外网访问,应用服务器所在的内网网段之间(web和db数据库之间访问丢包严重)不能互相访问 其他网段正常 2.怀疑是网络设备问题,将连接该网段设备的交换机重启后故障依旧,通过 ...
- Highcharts 使用
官网:https://www.hcharts.cn/ api:https://api.hcharts.cn/highcharts 效果 html代码 <div id="containe ...
- CSS和DIV
DIV主要就是结合CSS使用来对网页进行布局: CSS可以通过单独建立一个.css的文件来使用<link type="text/css" href="1.css& ...
- OneNET麒麟座应用开发之九:与SD卡通讯并保存数据
由于需要记录的数据量比较大,而且有些时候,有的用户不方便实时上传数据,所以要求使用SD卡存储数据然后人工收取上传.为此我们选择了一种通用的SD卡读写器. 1.读卡器简介 该读卡器整合 SD 卡规范和 ...
- 教你构建好 SpringBoot + SSM 框架
来源:Howie_Y https://juejin.im/post/5b53f677f265da0f8f203914 目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵 ...
- 搭建ssh框架项目(三)
一.创建业务层 (1)创建业务层接口IElecTextService.java package com.cppdy.ssh.service; import com.cppdy.ssh.domain.E ...
- 性能测试二十二:环境部署之Nginx
由于单纯用tomcat只能通过ip+端口号的形式访问,这样只能访问一个tomcat,而真实项目中又不可能只用一两个tomcat,所以就需要Nginx来进行分配访问请求, Nginx本身性能非常好,据官 ...
- ruby学习--条件控制
条件控制 本人喜欢用程序demo记录的方式来记录某方法的使用,如times方法,仅作个人学习记录 #--------------if语句(相反是unless)而while相同于until------- ...
- centos 7 增加网卡子接口配置
centos 7 增加网卡子接口配置 http://www.mamicode.com/info-detail-1351950.html