Understanding *args and **kwargs of s variable parameters in Python

Source: Internet
Author: User

Using variable parameters with default parameters

Python is the simplest way to support mutable parameters, such as using default parameters, such as:

Def Test_defargs (one, 2):   print ' Required argument: ', one   print ' Optional argument: ', Twotest_defargs (1) # result:# Required argument:1# Optional Argument:2test_defargs (1, 3) # result:# Required argument:1# Optional argument:3

Use *args and **kwargs when defining a function

Of course, this article focuses on a method to achieve variable parameters (Variable Argument): Using *args and **kwargs syntax. Where *args is a mutable list of positional arguments, **kwargs is a variable keyword arguments list. Also, *args must precede **kwargs, because positional arguments must precede keyword arguments.

The basic usage of both is introduced first.

The following example uses *args and contains a required parameter:

Def Test_args (First, *args):   print ' Required argument: ', first for   v in args:      print ' Optional argument: ', VT Est_args (1, 2, 3, 4) # result:# Required argument:1# Optional argument: Optional argument  :  3# Optional argument :  4

The following example uses *kwargs and contains a list of required parameters and *args:

Def Test_kwargs (First, *args, **kwargs):   print ' Required argument: ', first for   v in args:      print ' Optional arg  Ument (*args): ', V for   K, V in Kwargs.items ():      print ' Optional argument%s (*kwargs):%s '% (k, v) Test_kwargs (1, 2, 3, 4, k1=5, k2=6) # results:# Required argument: #  Optional argument (*args): *  Optional argument (*args): c13/>3# Optional argument (*args):  4# Optional argument K2 (*kwargs): 6# Optional argument K1 (*kwargs): 5

When a function is called, use *args and **kwargs

The *args and **kwargs grammars can be used not only in function definitions, but also in function calls. The difference is that if you use *args and **kwargs at the location where the function is defined is a process that takes the parameter pack, then it is a procedure to unpack the parameters when the function is called. The following example is used to deepen understanding:

Def Test_args (first, second, third, fourth, fifth):    print ' first argument: ', first    print ' second argument: ', sec Ond    print ' third argument: ', third    print ' fourth argument: ', fourth    print ' fifth argument: ', fifth# use *ar Gsargs = [1, 2, 3, 4, 5]test_args (*args) # results:# first argument: #  Second argument: third  argument:  3# Fourth argument:  4# fifth argument:  AA Use **kwargskwargs = {    ' first ': 1,    ' second ': 2,    ' third ': 3 ,    ' fourth ': 4,    ' fifth ': 5}test_args (**kwargs) # results:# first argument: #  Second argument:  * Third argument:  3# fourth argument:  4# fifth argument:  5

Functions can be easily defined using *args and **kwargs, and extensibility can be enhanced for future code maintenance.

Example

def foo (*args, **kwargs): print ('    args = ', args) print ('    Kwargs = ', Kwargs)    print ('----------------------- ----------------') if __name__ = = ' __main__ ':    foo (1,2,3,4)    foo (a=1,b=2,c=3)    foo (1,2,3,4, a=1,b=2,c=3)    foo (' A ', 1, None, a=1, b= ' 2 ', c=3)

Attention

Note: When defining or invoking this function, the following rules apply:
The variable parameter must be after the immutable parameter
*args is a mutable list of positional arguments, **kwargs is a variable keyword arguments list. Also, *args must be located before **kwargs, because positional arguments must be located before keyword arguments

  • Related Article

    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.