How to use *args and **kwargs in Python
Or, How to use variable length argument lists in Python.
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:", arg
test_var_args(1, "two", 3)
Results:
formal arg: 1 another arg: two another 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: 1 another keyword arg: myarg2: two another 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 whencalling a function.
def test_var_args_call(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
args = ("two", 3)
test_var_args_call(1, *args)
Results:
arg1: 1 arg2: two arg3: 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:", arg3
kwargs = {"arg3": 3, "arg2": "two"}
test_var_args_call(1, **kwargs)
Results:
arg1: 1 arg2: two arg3: 3
How to use *args and **kwargs in Python的更多相关文章
- *args和**kwargs在python中的作用
我发现PYTHON新手在理解*args和**kwargs这两个魔法变量的时候有些困难.他们到底是什么呢? 首先,我先告诉大家一件事情,完整地写*args和**kwargs是不必要的,我们可以只写*和* ...
- Python函数参数*args和**kwargs
1. Python中使用*args和**kwargs #!/usr/bin/env python3 # coding: utf-8 # File: args_kwargs_demo.py # Auth ...
- Python中 * 与 **, *args 与 **kwargs的用法
* 用于传递位置参数(positional argument) ** 用于传递关键字参数(keyword argument) 首先,先通过一个简单的例子来介绍 * 的用法: def add_funct ...
- 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)
详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类 Python中函数参数定义及调用函数时传参 ...
- python中应用*args 与**kwargs
这是Python函数可变参数 args及kwargs------->目的是:当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值. ...
- Python之路 day3 函数定义 *args及**kwargs
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa import time # def logger(): # time_format ...
- [翻译]PYTHON中如何使用*ARGS和**KWARGS
[翻译]Python中如何使用*args和**kwargs 函数定义 函数调用 不知道有没有人翻译了,看到了,很短,顺手一翻 原文地址 入口 或者可以叫做,在Python中如何使用可变长参数列表 函数 ...
- python 中*args 和 **kwargs
简单的可以理解为python 中给函数传递的可变参数,args 是 列表的形式.kwargs 是 key,value的形式,也就是python 中的字典. *args 必须出现在**kwargs 的前 ...
- python 的 *args 和 **kwargs
Python支持可变参数,通过*args和**kwargs来指定,示例如下: def test_kwargs(first, *args, **kwargs): print 'Required a ...
随机推荐
- Json在PHP与JS之间传输
1. JS-->PHP a). JS create Json <script> $(document).ready(function(){ /*--JS create Json--* ...
- windows安装rabbitmq
官网下载windows安装版本:http://www.rabbitmq.com/install-windows.html ,安装文件rabbitmq-server-3.6.5.exe 前提:安装erl ...
- .net一般处理程序(httphandler)实现文件下载功能
Handler文件代码如下: public class MDMExporterWeb : IHttpHandler { public void ProcessRequest(HttpContext c ...
- ubuntu
mongoChef: http://3t.io/mongochef/download/core/platform/#tab-id-3 背景色改成豆沙绿: /usr/share/themes/Ambia ...
- shell脚本: 备份mysql远程数据库并清除一个月之前的数据
hxingxing-backup.sh: date="$(date +"%Y-%m-%d")"mysqldump -u root -h localhost -p ...
- bzoj4692: Beautiful Spacing
先二分答案后dp 设\(su[n]\)为\(\sum_{1}^{n}xi[i]\) 设\(f[n]\)为1时表示第n个单次能做某一行的结尾,且之前的空格满足二分出来的答案. 考虑怎样的\(f[i]\) ...
- Eclipse使用Maven tomcat:run命令启动web项目时修改默认端口
- 自己常用的webstrom快捷键
1.ctrl+"d" 复制正行 2.ctrl+"y" 删除正行 3.ctrl+"-" 缩小整块 4.ctrl+"+" 扩 ...
- docker 初探之简单安装 ----Windows10
报错一 $ docker run hello-world Unable to find image 'hello-world:latest' locally Pulling repository do ...
- Linux下的压缩和解压缩命令——gzip/gunzip
gzip命令 gzip命令用来压缩文件.gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处".gz"扩展名. gzip是在Linux系统中经常使用的一个对文件进行压 ...