python 基础——*args和**kwargs
*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。
def func(one, *args, **kwargs):
print type(one)
print type(args)
print type(kwargs)
print one
print args
print kwargs func('hello', 1, 2, name='Bob', age='')
func(1, 1, 2, name='Bob', age='')
#输出
'''
<type 'str'>
<type 'tuple'>
<type 'dict'>
hello
(1, 2)
{'age': '10', 'name': 'Bob'}
<type 'int'>
<type 'tuple'>
<type 'dict'>
1
(1, 2)
{'age': '10', 'name': 'Bob'}
'''
注意:同时使用*args和**kwargs时,必须普通参数在最前面,*args参数列要在**kwargs前,像foo(a=1, b='2', c=3, a', 1, None, )这样调用的话,会提示语法错误“SyntaxError: non-keyword arg after keyword arg”
def func(*args, one, **kwargs):
print type(one)
print type(args)
print type(kwargs)
print one
print args
print kwargs #输出
'''
语法错误!
File "<string>", line 1
def func(*args, one, **kwargs):
^
SyntaxError: invalid syntax '''
借助 **kwargs 的特性,可以用此来创建字典
def kw_dict(**kwargs):
return kwargs
print kw_dict(a=1,b=2,c=3)
#输出
'''
{'a': 1, 'c': 3, 'b': 2}
'''
python 基础——*args和**kwargs的更多相关文章
- Python基础-*args和**kwargs魔法变量
在学习Python时,总会遇到*args和**kwargs这两个魔法变量,那么它们到底是什么? 首先,并不是必须写成*args和**kwargs.只有变量前面的*(星号)才是必须的,你也可以写成*va ...
- python的*args和**kwargs基础用法
*args表示任何多个无名参数,它是一个tuple **kwargs:传入的字典,就如:a=1,传入键值,默认就传入到**kwargs中,如下面代码: class FOO: def __init__( ...
- 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 ...
- Python中*args 和**kwargs的用法
当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值.还是直接来代码吧,废话少说[python] def fun_var_args(far ...
- 学习python的*args和 **kwargs
*args表示任何多个无名参数,它是一个tuple(元组):**kwargs表示关键字参数,它是一个dict(字典) def foo(*args, **kwargs): print 'args = ' ...
- Python的*args与**kwargs
当Python的函数的参数不确定时,可以使用*args与**kwargs来指代不定数量的参数. 两者的区别是,*args是个tuple(元组),而**kwargs是个dict(字典). 先通过代码来验 ...
- python 中 *args 和 **kwargs 的区别
在 python 中,*args 和 **kwargs 都代表 1个 或 多个 参数的意思.*args 传入tuple 类型的无名参数,而 **kwargs 传入的参数是 dict 类型.下文举例说明 ...
- Python : *args和**kwargs是什么东东呢?
def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '------------------- ...
随机推荐
- free 和 fclose
想到一个场景,具体代码如下 #include <stdio.h> #include <stdlib.h> int main(int argc, const char *argv ...
- [iOS微博项目 - 1.0] - 搭建基本框架
A.搭建基本环境 github: https://github.com/hellovoidworld/HVWWeibo 项目结构: 1.使用代码构建UI,不使用storyboard ...
- URAL 2066 Simple Expression (水题,暴力)
题意:给定三个数,让你放上+-*三种符号,使得他们的值最小. 析:没什么好说的,全算一下就好.肯定用不到加,因为是非负数. 代码如下: #pragma comment(linker, "/S ...
- MySQL Update语句用法
用一个表的某列值更新另外一个表的某列值的sql语句: update tableA a innner join tableB b on a.column_1 = b.column_1 set a.col ...
- 插入三层treeview代码
#region treetView加载 private void treeViewLoad() { DataView dv = navds.tbSiteKind.AsDataView(); treeV ...
- MATLAB remove outliers.
Answer by Richard Willey on 9 Jan 2012 Hi Michael MATLAB doesn't provide a specific function to remo ...
- 关于H-Fox 函数
........We arrive at the following results which provide the sine and cosine transforms of the H-fun ...
- jsp验证码点击刷新
<img src="<%=basePath%>manage/code" alt="验证码" height="20" ali ...
- linux 下httpd服务开机启动
分类: 网站搭建2011-01-07 05:52 1164人阅读 评论(0) 收藏 举报 linuxapache 我的apache安装目录在 /usr/local/apache 有2种方法可以设置开机 ...
- 教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员
(27)Throw hardware at the problem, not developers 招数27: 把问题仍给硬件而不是开发人员 As developers, we often want ...