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

4563人阅读 评论(0) 收藏 举报
分类:

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


2
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11351951次
    • 积分:86923
    • 等级:
    • 排名:第15名
    • 原创:698篇
    • 转载:169篇
    • 译文:16篇
    • 评论:3028条
    学习经历
    中科院、百度、创新工场、小米、阿里巴巴、米扑科技

    期货从业资格证(2017.03)
    基金从业资格证(2016.11)
    证券投资顾问证(2016.9)
    证券从业资格证(2016.6)
    系统架构设计师(2013)
    软件设计师(2008)
    CSDN创业专访

    程序员创业邦
    QQ群: 239292073
    青春,每一个有梦想的人

    资深产品经理人
    QQ群: 338142405
    思与行,人人都是产品经理

    爱脚本,爱技术
    QQ群: 320296250
    (已满)
    python、ruby、php、go、shell

    程序人生的平凡生活
    QQ群: 282297696
    (已满)
    汇聚百度、小米、微软、腾讯、
    创新工场、阿里巴巴、日本雅虎
    个人博客
    博客专栏
    博客公告
    本博客内容,由本人精心整理
    欢迎交流,欢迎转载,大家转载注明出处,禁止用于商业目的。

    文章分类