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 ...
随机推荐
- Python3学习笔记12-定义函数及调用
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段,能提高应用的模块性,和代码的重复利用率 Python提供了许多内建函数,比如print().也可以自己创建函数,这被叫做用户自定义函数 ...
- 【转】Visual Studio——多字节编码与Unicode码
多字节字符与宽字节字符 1) char与wchar_t 我们知道C++基本数据类型中表示字符的有两种:char.wchar_t. char叫多字节字符,一个char占一个字节,之所以叫多字节字符是因为 ...
- Linux关闭防火墙,开放端口
Centos/redhat系统: 开启防火墙 #systemctl start firewalld.service 停止firewall #systemctl stop firewalld.servi ...
- spring整合strus2的Hellowworld
比较笨,看了三遍才能理解敲对并正确运行: step: 1.建立web工程( Dynamic Web project)一定要勾上创建web.xml 2.导入jar包 这个就比较坑了,我查了有半个小时才查 ...
- Zookeeper单机安装部署与配置(二)
在上篇博客中简单介绍了Zookeeper的特点和应用场景,详情可参考:<Zookeeper简介(一)>,那么这篇博客我们介绍一下关于Zookeeper的单机模式安装步骤与配置. 环境准备 ...
- python 全栈开发,Day2(in,while else,格式化输出,逻辑运算符,int与bool转换,编码)
一.in的使用 in 操作符用于判断关键字是否存在于变量中 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. a = '男孩wusir ...
- 使用spring-boot-starter-data-jpa 怎么配置使运行时输出SQL语句
在 application.properties 中加入以下配置 spring.jpa.show-sql=true
- ERP简介(一)
ERP是针对物资资源管理(物流).人力资源管理(人流).财务资源管理(财流).信息资源管理(信息流)集成一体化的企业管理软件 一:系统模块简介:
- #11 UVA 10529 Dumb Bones
题意: 放一堆排,每放一张,有pa的概率让左边的全倒,有pb的概率让右边全倒 问在最优策略下,最少要放几张才能摆放出n张 1<=n<=1000 题解: 这题应该还是很经典的 首先是期望部分 ...
- 【深入spring】IoC容器的实现
本文乃学习整理参考而来 IoC概述: 在spring中,IoC容器实现了依赖控制反转,它可以再对象生成或初始化时直接将数据注入到对象中,也可以通过将对象引用注入到对象数据域中的方式来注入方法调用的依赖 ...