*args,**kwargs的使用方法
生活随笔
收集整理的這篇文章主要介紹了
*args,**kwargs的使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
title: '*args,**kwargs的使用方法'
date: 2017-11-25 16:09:50
tags:
category:
---
*args和**kargs是一種約定俗稱的用法,目的是用于傳入不定數量的參數,前者把傳入的參數變成一個tuple,后者把傳入的參數編程一個字典
In [1]: def foo(*args):...: for a in args:...: print a...: ...: In [2]: foo(1) 1In [4]: foo(1,2,3) 1 2 3The **kwargs will give you all keyword arguments except for those corresponding to a formal parameter as a dictionary.
In [5]: def bar(**kwargs):...: for a in kwargs:...: print a, kwargs[a]...: ...: In [6]: bar(name='one', age=27) age 27 name oneBoth idioms can be mixed with normal arguments to allow a set of fixed and some variable arguments:
def foo(kind, *args, **kwargs):passAnother usage of the *l idiom is to unpack argument lists when calling a function.
*的另外一個用途就是unpack(打開包裹),比如調用zip的時候,zip的用法是傳入n個對象進行zip,但是每一個都是單獨的,比如你可以zip(*[1,2,3])
In [9]: def foo(bar, lee):...: print bar, lee...: ...: In [10]: l = [1,2]In [11]: foo(*l) 1 2轉載于:https://www.cnblogs.com/drawon/p/8520718.html
總結
以上是生活随笔為你收集整理的*args,**kwargs的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj4025: 二分图
- 下一篇: Spring之使用外部属性文件