`
king_tt
  • 浏览: 2110336 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Python 学习入门(17)—— args, kwargs

 
阅读更多

The special syntax,*argsand**kwargsin function definitions is used to pass a variable number of arguments to a function. The single asterisk form (*args) is used to pass anon-keyworded, variable-length argument list, and the double asterisk form is used to pass akeyworded, 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*argsand**kwargswhencallinga function

This special syntax can be used, not only in function definitions, but also whencallinga 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

代码示例

#!/usr/bin/python
# -*- coding:utf-8 -*-
#
# http://blog.ithomer.net


# *args
def test1(farg, *args):
    print "formal arg", farg
    for arg in args:
        print "another arg:", arg

# **kwargs
def test2(farg, **kwargs):
    print "formal arg", farg
    for key in kwargs:
        print "another keyword arg: %s, %s" % (key, kwargs[key])

# *args
def test3(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

# **kwargs
def test4(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3


# *args
def test5(*args):
    for count, thing in enumerate(args):
        print "%d -> %s" % (count, thing)

# **kwargs
def test6(**kwargs):
    for name, value in kwargs.items():
        print name, "=", value

def test7(a, b, c):
    print "a=",a, "& b=", b, "& c=", c

if __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可变参数*args和**kwargs用法实例小结

    主要介绍了Python可变参数*args和**kwargs用法,结合实例形式总结分析了Python中可变参数*args和**kwargs的功能、区别与具体使用技巧,需要的朋友可以参考下

    Python *args和**kwargs用法实例解析

    主要介绍了Python *args和**kwargs用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    python函数参数*args**kwargs用法实例

    # 当函数的参数不确定时,可以使用*args和**kwargs。*args没有key值,**kwargs有key值 def fun_var_args(farg, *args): print ‘args:’, farg for value in args: print ‘another arg:’,value # *args可以当作...

    Python中*args和**kwargs的区别详解

    主要介绍了Python中*args和**kwargs的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Python 中的*args 和**kwargs

    Python 中的*args 和**kwargs的详细解释与理解

    Python库 | python-args-1.0.1.tar.gz

    python库。 资源全名:python-args-1.0.1.tar.gz

    基于python的OpenCV项目实战——数字识别

    模板:灰度处理-二值处理-轮廓检测-轮廓排序与数字对应上放到字典里 输入图像:灰度处理,礼帽处理,x梯度,闭操作,取轮廓,画到原图像上,得到轮廓的外接矩形,通过宽高比得到目标轮廓,目标轮廓取出,二值化,对...

    python3——argparse模块入门

    python37——argparse模块入门 以下内容来源于对python文档的学习 基础 下列函数不作任何事情 import argparse parser = argparse.ArgumentParser() parser.parse_args() #如下是运行结果 $ python3 prog.py $ ...

    Python入门09——函数的参数

    在Python中,函数的参数主要可以分为以下三种类型: 1、可变参数 可变参数必须放在固定参数之后。 >>> def fun(a,b,*args): print(a,b,args) >>> fun(1,2,3,4,5) 1 2 (3, 4, 5) 2、关键字参数 必须成对传值:key ...

    python参数传递的*args和**kwargs

    **kwargs:表示的就是形参中按照关键字传值把多余的传值以字典的方式呈现 主要是 用来表示函数接收可变长度的关键字参数列表作为函数的输入。  *args: 就是以元组的形式来呈现多余的参数,接受非关键字的参数 ...

    用实例说明python的*args和**kwargs用法

    先来看一个例子:复制代码 代码如下:>>> def foo(*args, **kwargs): print ‘args =’, args print ‘kwargs = ‘, kwargs print ‘———————–‘  >>> if __name__ == ‘__main__’: foo(1, 2, 3, 4) ...

    Python库 | pylint-args-0.0.5.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:pylint-args-0.0.5.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python函数中*args和**kwargs来传递变长参数的用法

    主要介绍了Python编程中使用*args和**kwargs来传递可变参数的用法,文中举了变长参数的例子,需要的朋友可以参考下

    Python库 | kwonly_args-1.0.2-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:kwonly_args-1.0.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | kwonly_args-1.0.3-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:kwonly_args-1.0.3-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

Global site tag (gtag.js) - Google Analytics