Understanding *args and **kwargs in Python

Source: Internet
Author: User

Transferred from: Http://kodango.com/variable-arguments-in-python

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

DefTest_defargs(One,Both= 2): Print  ' Required argument: ' , one print  ' Optional argument: '  Twotest_defargs (1< Span class= "com" ># result:# Required argument:1 # Optional argument:2test_defargs (1, 3) # Result:# Required argument:1# Optional Argument:3                

Of course, the topic in this article is not about default parameters, but another way 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:

DefTest_args(First, *Args): Print ' Required argument: ',Firstfor v in Args: print  ' Optional argument: ' ,< Span class= "PLN" > Vtest_args (1, 2, 3, Span class= "lit" >4) # result: # Required Argument:1# Optional argument:2 # Optional Argument:3# Optional argument:4      /span>                

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

DefTest_kwargs(First, *Args, **Kwargs): Print ' Required argument: ',FirstForVInchArgs: Print ' Optional argument (*args): ',VForK,VInchKwargs.Items(): Print ' Optional argument%s (*kwargs):%s ' % (K,V)Test_kwargs(1, 2, 3 , 4, K1 =5, K2= 6) # results: # Required Argument:1# Optional argument (*args): 2# Optional argument (*args): 3# Optional argument (*args): 4 # Optional argument K2 (*kwargs): 6# Optional argument K1 (*kwargs): 5             

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:

DefTest_args(First,Second,Third,Fourth,Fifth): Print ' First argument: ',FirstPrint ' Second argument: ',SecondPrint ' Third argument: ',ThirdPrint ' Fourth argument: ',FourthPrint ' Fifth argument: ',Fifth# Use *argsArgs= [1, 2, 3, 4, 5]Test_args(*Args)# results:# First Argument:1# Second Argument:2# Third Argument:3# Fourth Argument:4# Fifth Argument:5# Use **kwargsKwargs= { ' First ': 1, ' Second ': 2, ' Third ': 3, : 4,  ' fifth ' : 5}test_args (**kwargs# results:# first argument:1 # Second argument:2# 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.

Understanding *args and **kwargs in Python

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.