4.5冗余参数处理
2014-02-13 01:25
330 查看
4.5冗余参数处理
(1)、多类型传值(向函数传元组和字典)
example4.5.1
传递元组fun(*args)
传递字典fun(**kwords)<一>
example4.5.5
传递字典fun(**kwords)<二>
def fun(*args,**kw)
多参数的接收:
example4.5.6
注:学习内容来源于网易云课堂《疯狂的Python:快速入门精讲》;代码执行环境为Win;Python版本为 3.3.3
(1)、多类型传值(向函数传元组和字典)
example4.5.1
>>> def f(x): print(x) >>> #传递元组 >>> f(range(10)) range(0, 10) >>> f([1,2,3,4]) [1, 2, 3, 4] >>> #传递字典 >>> f({1:111,2:222,3:333}) {1: 111, 2: 222, 3: 333}example4.5.2
>>> def f(x,y): print(x,y) >>> t=('a','b') >>> f(t) Traceback (most recent call last): File "<pyshell#94>", line 1, in <module> f(t) TypeError: f() missing 1 required positional argument: 'y' >>> f(t,'test') ('a', 'b') test >>>example4.5.3
#格式化字符串说明 >>> print("%s:%s"%(13,14)) 13:14example4.5.3
传递元组fun(*args)
>>> def f(x,y): print("%s:%s"%(x,y)) >>> t = ('name','milo') >>> f(t) Traceback (most recent call last): File "<pyshell#104>", line 1, in <module> f(t) TypeError: f() missing 1 required positional argument: 'y' >>> #传递元组fun(*args) >>> f(*t) name:miloexample4.5.4
传递字典fun(**kwords)<一>
>>> def f(name='name',age=0): print("name:%s"%name) print("age:%s"%age) >>> f() name:name age:0 >>> f("milo",30) name:milo age:30 >>> t(30,'milo') Traceback (most recent call last): File "<pyshell#118>", line 1, in <module> t(30,'milo') TypeError: 'tuple' object is not callable >>> t=(30,'milo') >>> f(*t) name:30 age:milo >>> f(age = 30,name='milo') name:milo age:30 >>> #相同的实现 >>> #传递字典fun(**kwords) >>> d = {'age':30,'name':'milo'} >>> d {'name': 'milo', 'age': 30} >>> f(**d) name:milo age:30 >>>
example4.5.5
传递字典fun(**kwords)<二>
>>> def f(name='name',age=0): print("name:%s"%name) print("age:%s"%age) >>> #当实参和行参名称一一不对应的时候,传递字典fun(**kwords) >>> d = {'age':30,'name':'milo'} >>> d {'name': 'milo', 'age': 30} >>> f(**d) name:milo age:30 >>> d {'name': 'milo', 'age': 30} >>> d {'a':30,'n':'milo'} SyntaxError: invalid syntax >>> d={'a':30,'n':'milo'} >>> f(**d) Traceback (most recent call last): File "<pyshell#129>", line 1, in <module> f(**d) TypeError: f() got an unexpected keyword argument 'n' >>> #解决办法 >>> f(d['n'],d['a']) name:milo age:30 >>>(2)、处理多余实参(传值冗余)
def fun(*args,**kw)
多参数的接收:
example4.5.6
>>> def f(x,*args): print(x) print (args) >>> f(1) 1 () >>> f(1,2,3,4,5,6) 1 (2, 3, 4, 5, 6) >>> f(x=4) 4 () >>> #出现冗余参数 >>> #冗余参数 >>> f(x=4,y=40) Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> f(x=4,y=40) TypeError: f() got an unexpected keyword argument 'y' >>> def f(x,*args,**kwargs): print(x) print(*args) print(**kwargs) >>>#出现冗余参数的解决办法 >>> def f(x,*args,**kwargs) SyntaxError: invalid syntax >>> def f(x,*args,**kwargs): print (x) print (args) print (kwargs) >>> f(1) 1 () {} >>> f(1,2,3,4,5) 1 (2, 3, 4, 5) {} >>> f(x=1,y=2) 1 () {'y': 2} >>> f(1,2,3,4,5,6,x=10,y=20,z=30) Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> f(1,2,3,4,5,6,x=10,y=20,z=30) TypeError: f() got multiple values for argument 'x' >>> f(1,2,3,4,5,6,y=20,z=30) 1 (2, 3, 4, 5, 6) {'y': 20, 'z': 30} >>>
注:学习内容来源于网易云课堂《疯狂的Python:快速入门精讲》;代码执行环境为Win;Python版本为 3.3.3
相关文章推荐
- 部分电脑无法访问页面的问题处理,timestamps参数 rhel6
- springMVC的参数绑定和返回值类型封装处理原理解析
- 脚本乐园 Shell中命令行选项和参数的处理
- struts2请求参数处理
- HTML 传递参数的处理
- 信号发射过程&参数处理
- 远程方法动态调用之参数的处理
- Fortran C# 混合编程时,数组参数的处理
- 批处理调用exe文件,并给exe传入参数的方法
- 【Oracle】数据库启动阶段参数文件、控制文件的问题处理
- JavaScript中方法参数为URL处理办法
- php利用参数,分时处理大量数据
- Struts1.x和Struts2在处理请求参数上区别
- Mybatis源码分析之参数映射及处理ParameterHandler
- ora 32021 设置参数时参数值长度超过255处理办法
- 调用Win32 API如何处理指针类型的参数(二)
- JS通过URL传递参数时出现乱码的处理
- setsockopt()用法(参数详细说明) recv、send的超时处理
- 调用子程序时参数含有指针该如何处理
- 无论url请求什么.都可以拼接class类名.实例化.传递get参数-->给当前控制器-->传递给抽象父类-->都交给抽象父类.这个方法去处理call_user_func_array()