*args and **kw in Python

Source: Internet
Author: User
Tags unpack

When learning the Python adorner decorator, I encountered *args and **kw two function values passed.

To define functions in Python, you can use general parameters, default parameters, non-keyword parameters, and keyword parameters.

General parameters and default parameters in the previous study we have encountered, and *args and **kw are non-keyword parameters and keyword parameters, the latter are also variable parameters.

A non-keyword parameter is characterized by an asterisk * plus a parameter name, such as *number, which, when defined, can receive any number of parameters and store them in a tuple.

The keyword parameter is characterized by two asterisk * * plus the parameter name, such as **KW, which, when defined, saves any number of parameters received by KW to a dict. The keyword parameter is not necessarily passed in order in the delivery composition (because Key-value within dict is not sequential), but it must be provided as a parameter in the form of pass parameter name = Pass argument value.

Such as:

def try_it (*args, * *kw)     :print'args:', args      print'kw:', kw try_it (1,2,3,4, a=1,b=2,c=3) try_it (  'a', 1, None, a=1, b='2', c=3)

Operation Result:

Args: (1, 2, 3, 4) kw: {'a': 1,'C': 3,'b': 2} args: ('a', 1, None) kw: {'a': 1,'C': 3,'b':'2'}
python中的一般参数、默认参数、非关键字参数和关键字参数可以一起使用,或者只用其中某些,但是请注意,参数定义的顺序必须是:一般参数、默认参数、可变参数和关键字参数,先后顺序不能颠倒。即:
def func (A, B, c=0, *args, **kw):    Pass

Example:

We use the function ' Calculate_sum ', but ' calculate_sum ' requires multiple positional parameters to be passed to ' args ' as tuples. So the function ' ignore_first_calculate_sum ' needs to split the tuple ' Iargs ' and then pass the element as a positional parameter to ' calculate_sum '. Notice that the '*' is used to split the tuple.

So, we call ' Required_sum=calculate_sum (*Iargs) '.

' Required_sum=calculate_sum (Iargs) ' cannot be called because we need to unpack the value before passing it to ' calculate_sum '. Do not use '*' will not unpack the value, you will not be able to perform the desired action.

def calculate_sum (*args):     return  sum (args)def ignore_first_ Calculate_sum (a,*Iargs):     = calculate_sum (*Iargs)     print (" ",required_sum) ignore_first_calculate_sum (12, 1,4,5)  

Reference: CSDN

*args and **kw 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.