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 ...
随机推荐
- Android手机清除微信缓存
方法一: 1.任意找一个微信好友,给他发送网址 http://debugx5.qq.com 2.自己点击这个网址跳转 3.进入后看到下面的页面,通过勾选第二张截图的Cookie和文件缓存来清除微信缓存 ...
- [实战]MVC5+EF6+MySql企业网盘实战(29)——更新日志
摘要 NetDisk更新日志,及项目使用说明. 开发工具 Vs2013+mysql+ef6+mvc5 bug 1.在加载列表的时候,默认加载的所有,修改为,过滤逻辑删除的文件. 2.加载音乐,文档等分 ...
- SQL Server群集知识介绍
集群CLUSTER种类介绍 基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装 SQL Server群集如何在线检测 群集中的MS DTC分布式事务协调器 一.SQL Se ...
- Oracle操作
1.查询表空间地址: select name from v$datafile; 2.创建表空间: create tablespace yysspace datafile ‘D:\APP\MIAO\OR ...
- R语言:ggplot2精细化绘图——以实用商业化图表绘图为例
本文版权归http://www.cnblogs.com/weibaar 本文旨在介绍R语言中ggplot2包的一些精细化操作,主要适用于对R画图有一定了解,需要更精细化作图的人,尤其是那些刚从exce ...
- [Machine Learning & Algorithm]CAML机器学习系列2:深入浅出ML之Entropy-Based家族
声明:本博客整理自博友@zhouyong计算广告与机器学习-技术共享平台,尊重原创,欢迎感兴趣的博友查看原文. 写在前面 记得在<Pattern Recognition And Machine ...
- CentOS 6.5 编译 PHP-7 报错:undefined reference to `libiconv_open 无法编译 PHP libiconv
./configure --with-mysql=/backup/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zli ...
- Java获取某年第一天和最后一天
package com.dada.test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.uti ...
- runtime 第四部分method swizzling
接上一篇 http://www.cnblogs.com/ddavidXu/p/5924597.html 转载来源http://www.jianshu.com/p/6b905584f536 http:/ ...
- ubuntu系统theano和keras的安装
说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...