python如何定义不定参数函数
2015-10-29 14:58
766 查看
*args,可以传入任意多个参数 **args,以字典形式传入任意多个参数
元组形式:
1、定义函数
def test1(*args):
print(‘################test1################’)
print(type(args))
print(args)
2、调用函数
正确调用:
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’
python系列:[3]如何定义不定参数函数步骤阅读
python系列:[3]如何定义不定参数函数
2
字典形式:
1、定义函数
def test2(**kargs):
print(‘################test2################’)
print(type(kargs))
print(kargs)
2、调用函数
正确调用:
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
python系列:[3]如何定义不定参数函数
python系列:[3]如何定义不定参数函数
3
混合形式:
1、定义函数
def test3(*args, **kargs):
print(‘################test3################’)
print(type(args))
print(args)
print(type(kargs))
print(kargs)
2、调用函数
正确调用:
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
python系列:[3]如何定义不定参数函数
python系列:[3]如何定义不定参数函数
4
其他形式1:
1、定义函数
def test4(a = ()):
print(‘################test4################’)
print(type(a))
print(a)
2、调用函数
正确调用:
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:
1、定义函数
def test5(b = {}):
print(‘################test5################’)
print(type(b))
print(b)
2、调用函数
正确调用:
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:
1、定义函数
def test6(a = (), b = {}):
print(‘################test6################’)
print(type(a))
print(a)
print(type(b))
print(b)
2、调用函数
正确调用:
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’
相关文章推荐
- Python 中的join()与split()
- 性能测试培训: 监控CPU之python
- python 升级导致yum无法正常使用
- 基于Python实现自动慢查询分析,邮件自动发送
- 基于Python实现自动慢查询分析,邮件自动发送
- python+flask+SAE 微信公共平台开发的小小的demo
- python装饰器
- 近期面试遇到的问题以及总结(python和openstack)
- python安装pycrypto
- 使用jinja2生成配置文件
- Python练习册,第 0003 题
- win_python+python_opencv安装配置
- Python练习册,第 0002 题
- Python练习册,第 0000 题
- python调用解释器
- Python核心编程
- python 结合 selenium 使用iedriver报错问题处理
- Python BeautifulSoup 实战演练
- RedHat升级Python到2.7.6
- RedHat升级Python到2.7.6