Python * args ** kwargs, pythonargskwargs

Source: Internet
Author: User

Python * args ** kwargs, pythonargskwargs

To put it simply, * args and ** kwargs will be used when you cannot determine the number of input parameters. Here, the asterisk is just a code name behind it. You can also write it as.

The difference between the two is that the latter is required for key-value pairs, and the other is true for the former.

You can also use (a, * args, ** kwargs)

 

For specific examples, see the following code:

In [1]: def h(a):   ...:     print a   ...:     In [2]: h(3)3In [3]: def h(*args):   ...:     for i in args:   ...:         print i   ...:         In [4]: h((1, 2, 3))(1, 2, 3)In [5]: a = [1, 2, 3]In [6]: h(a)[1, 2, 3]In [7]: h(3)3In [8]: h(3, 3)33In [9]: h(3, 5)35In [10]: def h(a, *args):   ....:     print a   ....:     for i in args:   ....:         print i   ....:         In [11]: h(3, 4, 5)345In [12]: h(3, a)3[1, 2, 3]In [13]: h(3, (1, 2, 3))3(1, 2, 3)In [14]: h(a)[1, 2, 3]In [15]: h(a, a)[1, 2, 3][1, 2, 3]In [16]: def h(a):   ....:     for i in a:   ....:         print i   ....:         In [17]: h(a)123

  

In [19]: def h(a, *args):    print 'a', a    for i in args:        print i   ....:         In [20]: h(1, 3, 4)a 134In [21]: h(1, a)a 1[1, 2, 3]In [22]: h(1, (1, (2, 3)))a 1(1, (2, 3))In [23]: h(1, (1, 2, 3))a 1(1, 2, 3)In [24]: h(1, (1, 2, 3), (3, 4))a 1(1, 2, 3)(3, 4)

Note: The result is not correctly printed here. Instead, the number in parentheses is not typed by line, but directly. The correct usage is to add *. See the following code.

** Kwargs usage:

In [25]: def h (** kwargs ):....: for k, v in kwargs. items ():....: print k, v ....: In [26]: h ({'A': 1, 'B': 3}) handle TypeError Traceback (most recent call last) <ipython-input-26-557f772fd853> in <module> () ----> 1 h ({'A': 1, 'B': 3}) # No ** here, error TypeError: h () takes exactly 0 arguments (1 given) in [27]: h (** {'A': 1, 'B': 3}) a 1b 3In [28]: def hh (* args) :....: for I in args :....: print I ....: In [29]: hh (* (1, 2, 3) # Here is the correct method of use 123In [30]: a = (1, 2, 3) in [31]: hh (* a) 123In [32]: def hhh (a, * args ):....: print a, 'A '....: for I in args :....: print I ....: In [33]: hhh (* (1, 3, 4) 1 a34

 

In the same order, def f (a, * args, ** kwargs)

In [34]: def hhhh(a, *args, **kwargs):   ....:     print 'a:', a   ....:     for i in args:   ....:         print i   ....:     print 'kwargs:'   ....:     for k, v in kwargs:   ....:         print k, v   ....:         In [35]: hhhh(1, *(2, 3), **{'c': 1, 'b': 3})a: 123kwargs:---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-35-6ac13cc71458> in <module>()----> 1 hhhh(1, *(2, 3), **{'c': 1, 'b': 3})<ipython-input-34-18188bbea105> in hhhh(a, *args, **kwargs)      4         print i      5     print 'kwargs:'----> 6     for k, v in kwargs:      7         print k, v      8 ValueError: need more than 1 value to unpackIn [36]: def hhhh(a, *args, **kwargs):    print 'a:', a    for i in args:        print i    print 'kwargs:'    for k, v in kwargs.items:        print k, v   ....:         In [37]: hhhh(1, *(2, 3), **{'c': 1, 'b': 3})a: 123kwargs:---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-37-6ac13cc71458> in <module>()----> 1 hhhh(1, *(2, 3), **{'c': 1, 'b': 3})<ipython-input-36-0e0a773cdf48> in hhhh(a, *args, **kwargs)      4         print i      5     print 'kwargs:'----> 6     for k, v in kwargs.items:      7         print k, v      8 TypeError: 'builtin_function_or_method' object is not iterableIn [38]: def hhhh(a, *args, **kwargs):    print 'a:', a    for i in args:        print i    print 'kwargs:'    for k, v in kwargs.items():        print k, v   ....:         In [39]: hhhh(1, *(2, 3), **{'c': 1, 'b': 3})a: 123kwargs:c 1b 3

Note: add () to items ()

 

See:

Https://eastlakeside.gitbooks.io/interpy-zh/content/args_kwargs/When_to_use.html

Https://docs.python.org/2/tutorial/controlflow.html#defining-functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.