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

原创 2013年12月05日 23:32:34

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 when calling 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

代码示例

#!/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

""" 可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple 关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict """# ...

Python中的*args和**kwargs

Python函数参数中*args和**kwargs的理解及简单使用

python中的*args和**kwargs

我们在python函数的形参中经常能发现这两个参数的身影。比如multiprocessing.Process, MySQLdb.connection。 1. *args做为函数的形参,表示不定数目的...

Python 中的 *args 和 **kwargs 参数

Python 中的 *args 和 **kwargs 参数*args 语法使用 *args 语法,一个 python 函数可以在它的形式参数列表中声明接受零个或多个位置参数。 *args 将所有可变的...

python字典(dictionary)使用:基本函数code实例,字典的合并、排序、copy,函数中*args 和**kwargs做形参和实参

python字典dictionary,以前有过介绍,现就几个不常用函数写测试例子, 字典声明如,a={}; [python]dictionary方法说明:参考:http://blog.csdn.net...

Python中如何使用*args 和 **kwargs (非keyworded,可变长度参数列表 keyworded形式可变长度的参数列表)

Python中如何使用*args 和 **kwargs 也可以说是如何在Python中使用变长的参数列表。 在最近阅读RYU的程序的时候,一直会看到*args和**kwargs这两...

理解 Python 中的 *args 和 **kwargs

Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def test_defargs(one, two = 2): print 'Required argument: ', ...
  • tiokld
  • tiokld
  • 2017年04月28日 15:54
  • 133

Python函数参数传递的*args和**kwargs

python函数定义中的*args和**kwargs

python中的可变参数*args, **kwargs

def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '-------------------...

Python 中的 *args 和 **kwargs

透彻分析了 def fun(*args, **kwargs) 这种函数定义的形式的内部原理,深入剖析了函数参数的4种形式。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Python 学习入门(17)—— args, kwargs
举报原因:
原因补充:

(最多只允许输入30个字)