Python 学习入门(17)—— args, kwargs
来源:互联网 发布:npm node sass 版本 编辑:程序博客网 时间:2024/06/06 02:33
The special syntax, *args
and **kwargs
in function definitions is used to pass a variable number of arguments to a function. The single asterisk form (*args
) is used to pass a non-keyworded, variable-length argument list, and the double asterisk form is used to pass a keyworded, variable-length argument list. Here is an example of how to use the non-keyworded form. This example passes one formal (positional) argument, and two more variable length arguments.
def test_var_args(farg, *args): print "formal arg:", farg for arg in args: print "another arg:", argtest_var_args(1, "two", 3)
Results:
formal arg: 1another arg: twoanother arg: 3
Here is an example of how to use the keyworded form. Again, one formal argument and two keyworded variable arguments are passed.
def test_var_kwargs(farg, **kwargs): print "formal arg:", farg for key in kwargs: print "another keyword arg: %s: %s" % (key, kwargs[key])test_var_kwargs(farg=1, myarg2="two", myarg3=3)
Results:
formal arg: 1another keyword arg: myarg2: twoanother keyword arg: myarg3: 3
Using *args
and **kwargs
when calling a function
This special syntax can be used, not only in function definitions, but also when calling a function.
def test_var_args_call(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3args = ("two", 3)test_var_args_call(1, *args)
Results:
arg1: 1arg2: twoarg3: 3
Here is an example using the keyworded form when calling a function:
def test_var_args_call(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3kwargs = {"arg3": 3, "arg2": "two"}test_var_args_call(1, **kwargs)
Results:
arg1: 1arg2: twoarg3: 3
#!/usr/bin/python# -*- coding:utf-8 -*-## http://blog.ithomer.net# *argsdef test1(farg, *args): print "formal arg", farg for arg in args: print "another arg:", arg# **kwargsdef test2(farg, **kwargs): print "formal arg", farg for key in kwargs: print "another keyword arg: %s, %s" % (key, kwargs[key])# *argsdef test3(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3# **kwargsdef test4(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3# *argsdef test5(*args): for count, thing in enumerate(args): print "%d -> %s" % (count, thing)# **kwargsdef test6(**kwargs): for name, value in kwargs.items(): print name, "=", valuedef test7(a, b, c): print "a=",a, "& b=", b, "& c=", cif __name__ == "__main__": test1(1, "two", 3) test2(farg=1, myarg2='two', myargs=3) args = ("two", 3) test3(1, *args) kwargs = {"arg3":3, "arg2":"two"} #test4(1, **kwargs) test4(arg1=1, **kwargs) test5("apple", "banana", "cabbage") test6(apple="fruit", cabbage="vagetable") mylist = ['aa', 'bb', 'cc'] test7(*mylist)运行结果:
formal arg 1
another arg: two
another arg: 3
formal arg 1
another keyword arg: myarg2, two
another keyword arg: myargs, 3
arg1: 1
arg2: two
arg3: 3
arg1: 1
arg2: two
arg3: 3
0 -> apple
1 -> banana
2 -> cabbage
cabbage = vagetable
apple = fruit
a= aa & b= bb & c= cc
参考推荐:
How to use *args and **kwargs in Python
python *args and * kwargs
- Python 学习入门(17)—— args, kwargs
- Python 学习入门(17)—— args, kwargs
- python 学习笔记——参数 *args **kwargs
- Python学习之路 —— *args 和**kwargs
- Python进阶—*args与**kwargs详解
- python学习:装饰器,*args **kwargs
- python学习之*args 和 **kwargs
- Python学习之*args和**kwargs
- Python *args 和 **kwargs
- python *args and * kwargs
- 【python】args kwargs 用法
- python *args和**kwargs
- python *args **kwargs
- python *args **kwargs
- 【PYTHON】*args **kwargs
- Python args kwargs
- python *args,**kwargs使用
- python *args and **kwargs
- 关于在windows系统下远程控制mac系统
- AndroidのUI设计研究(一)——自定义ProgressBar
- VC 利用DLL共享区间在进程间共享数据及进程间广播消息
- 10g RAC重建ASM以及相关报错解决
- 如何理解Hadoop-Hbase原理与应用小结
- Python 学习入门(17)—— args, kwargs
- 调用Nero SDK 刻录光盘(含代码)
- unity3D,NGUI中button响应事件实现
- 11gR2启动ASM实例时遭遇ORA-29701
- AAA 及RADIUS协议配置
- 2014 OLTP Memory Database lock 测试
- 可穿戴技术的10 大设计原则
- linux内核 64位 X86_64 地址空间
- ASSERT_VALID和ASSERT宏分析