函数冗余参数
来源:互联网 发布:新西兰旅游 知乎 编辑:程序博客网 时间:2024/06/03 16:33
多类型传值
- list类型给函数参数传值
- tuple类型给函数参数传值
- dictionary类型给函数参数传值
注意:list和tuple类型给函数参数传值的时候,元素的个数必须与函数参数的个数相等,且参数的顺序与list和tuple中的元素的顺序是完全一样的;dictionary类型给函数参数传值的时候,元素个数必须与函数的参数个数相等,且key的名字与函数中的参数的名字完全相同,字典中参数的顺序不作要求。
#/usr/bin/python#coding:utf8def fun(name, age) : print "name is %s" %name print "age is %d" %agel = ['Gudio', 35] fun(*l) #list给函数参数传值print "-"*20t = ('Bob',11) fun(*t) #tuple给函数参数传值print "-"*20d = {"age":28, "name":"Brad"}fun(**d) #dict给函数参数传值print "-"*20d1 = {"age1":28, "name1":"Brad"}#fun(**d1) #这里是错误的,因为字典的key与函数行参的名字不能一一对应print "-"*2023行注释之前的输出结果如下:name is Gudioage is 35--------------------name is Bobage is 11--------------------name is Bradage is 28--------------------Traceback (most recent call last): File "13.py", line 23, in <module> fun(**d1) #这里是错误的,因为字典的key与函数行参的名字不能一一对应TypeError: fun() got an unexpected keyword argument 'age1'23行注释之后的输出结果如下:name is Gudioage is 35--------------------name is Bobage is 11--------------------name is Bradage is 28----------------------------------------
传值冗余,参数个数任意
python的函数的参数个数可以固定,也可以参数个数是任意个数,如下所示:
#!/usr/bin/python#coding:utf8def fun(*args): # 参数个数任意 print argst = (10, 21, 33)fun(*t)print '#'*20fun() # 不传任意参数print '#'*20l = [1, 2, 3]fun(*l)print '#'*20fun(100, 200, 300)print '#'*20def fun1(x , *args): #至少一个参数 print "x = ", x print argsfun1(10, *t) # 10赋值给x 元组t赋值给argsprint '#'*20fun1(10, 11, 12, 13) #按照顺序,10赋值给x,11、12和13自动当作一个元组print '#'*20fun1(10) #按照顺序,10赋值给x,元组为空print '#'*20#以下是指定参数传值,有元组传值的时候,不可以指定传值,指定的话就需要字典来传值了def fun2(**args): #参数个数为任意个数 print argsd = {'name':'Manni', 'age': 30}fun2() #不传参数print '#'*20fun2(**d)print '#'*20fun2(x = 10, **d)print '#'*20def fun3(x, **args): print "x = ", x print "args = ", argsfun3(x = 10, y = 12)print '#'*20fun3(x = 10, **d)print '#'*20
0 0
- 函数冗余参数
- 11-python_函数-冗余参数
- 4.5冗余参数处理
- python的冗余参数
- 中谷教育Python19笔记——函数之冗余参数
- python中消除冗余参数的方法
- 冗余
- 冗余
- 参数列表中的self:显式不等于冗余
- 参数列表中的self:显式不等于冗余
- python 参数列表中的self 显式不等于冗余
- 在webxml中进行数据库参数配置,减少代码冗余
- 参数列表中的self:显式不等于冗余
- 复习4次课(12月1日) 1.1 函数定义 1.2 函数的参数 1.3 函数的默认参数 1.4 函数的变量 1.5 函数的返回值 1.6 多类型传值和冗余参数 1.7 函数的递归调用
- 函数参数和参数
- 函数参数
- 函数参数
- 函数参数
- 如何用tomcat配置虚拟目录,方法要详细明了
- Hibernate懒加载
- poj 3580 splay
- JAVA实现链表中倒数第K个节点问题(《剑指offer》)
- 《php和mysql web开发》笔记——第10章 使用MySQL数据库
- 函数冗余参数
- Non-negative Partial Sums(单调队列,好题)
- Bundle Adjustment 光束法平差详解
- Node.js第三方库之request
- ocp-34
- ZOJ 3829 Known Notation
- CodeForces 416C Booking System
- ocp-35
- ESP8266-01细玩笔记(二)