专栏首页米扑专栏Python 学习入门(17)—— args, kwargs

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • TCP三次握手/四次挥手

    TCP 连接是通过三次握手进行初始化的。三次握手的目的是同步连接双方的序列号和确认号并交换 TCP 窗口大小信息。以下步骤概述了通常情况下客户端计算机联系服务器...

    阳光岛主
  • Python 学习入门(2)—— 数组

    Python,内置丰富的数据类型。与Java、C++相比,这些数据类型有效地减少代码的长度。下面这个列表简要地描述了Python内置数据类型(适用于Python...

    阳光岛主
  • linux awk 数组和循环

    awk 中数组叫做关联数组(associative arrays),下标可以是数字也可以是字符串。awk 中的数组不必提前声明,也不必声明大小,初始化数组元素用...

    阳光岛主
  • 函数的不定长参数

    skylark
  • *args 和 **kwargs的用法

    一 简介 *args 和 **kwargs 主要用于函数定义。 当我们需要定义的函数的传入参数个数不确定时,可以使用*args 和 **kwargs ...

    用户1278550
  • Java面试题:Java中的集合及其继承关系

    关于集合的体系是每个人都应该烂熟于心的,尤其是对我们经常使用的List,Map的原理更该如此.这里我们看这张图即可:

    李红
  • Angel推荐算法在游戏推荐中的应用

    ? 文章作者:王培军 腾讯 高级工程师 整理编辑:李沛欣 出品平台:DataFunTalk、AI启蒙者 导读:Angel是腾讯自研的分布式高性能的机器学习平...

    腾讯大数据
  • ROS(indigo)机器人操作系统学习有趣丰富的Gazebo仿真示例evarobot

    通过网页快速了解Linux(Ubuntu)和ROS机器人操作系统,请参考实验楼在线系统如下:

    zhangrelay
  • set集合python_python set集合

    参考链接: Python 集合set | symmetric_difference

    用户7886150
  • 硬核还原:显微镜手撸晶体管,逆向工程还原经典计算器

    它很受欢迎,自1974年发售,就频频出现在《大众机械》等出版物封面。其巧妙编写的固件,使它本只用于基础算术的处理器,能马力倍开远远超出正常性能。这也使得Sinc...

    大数据文摘

扫码关注云+社区

领取腾讯云代金券