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 ...
随机推荐
- System.Runtime.InteropServices.COMException (0x800A03EC): 无法访问文件
使用Microsoft.Office.Interop.Excel 操作 今天在服务器部署,操作程序csv文件转xsl文件的时候,遇到一下问题: System.Runtime.InteropServic ...
- C++:greater<int>和less<int>
greater和less是xfunctional.h中的两个结构体,代码如下: template<class _Ty = void> struct less { // functor fo ...
- Android系统信息(内存、cpu、sd卡、电量、版本)获取
Android系统信息(内存.cpu.sd卡.电量.版本)获取 /*APPInfo.java*/ public class AppInfo { private String appLable; pri ...
- npm下载速度过慢的解决办法
第一种方式: 在cmd 输入指令:npm config set registry https://registry.npm.taobao.org 不建议使用cnpm! 设置完后,注意检查:输入指令:n ...
- 微信HTML5页面设计建议
一个HTML5页面从提出到完成上线的流程:> 1.需求方.设计人员.H5实现人员三方共同讨论实现方案 2.设计人员出设计图 3.H5人员按设计图出H5页面 4.需求方评估已实现的H5页面后给 ...
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- DOMContentLoaded与load的区别
声明:此文章为转载(点击查看原文),如有侵权24小时内删除.联系QQ:1522025433. (1)在chrome浏览器的开发过程中,我们会看到network面板中有这两个数值,分别对应网 络请求上的 ...
- js中字符串概念
字符串概念:所有带单引号和双引号的叫做字符串 字符串的数据类型:字符串既是基本数据类型,又是复合数据类型. 字符串存储在内存里[只读数据段]的地方.字符串的变量里存储的是字符串的地址. [注]使用起来 ...
- Python 多环境配置管理
一.概述 实际工程开发中常常会对开发.测试和生产等不同环境配置不同的数据库环境,传统方式可以通过添加不同环境的配置文件达到部署时的动态切换的效果.这种方式还不错,不过不同环境间往往会共享相同的配置而造 ...
- 获取更新元素文本text()
text() 方法,获取元素文本,也可以设置元素的文本值.相 <!DOCTYPE html> <html lang="en"> <head> & ...