The *arg and **kwarg__python in Python

Source: Internet
Author: User
Tags unpack
a simple function

First we can set a simple function that only considers the Required_arg ( positional parameter ) within the function.

def exmaple (required_arg):
    print Required_arg

exmaple ("Hello, world!")

>> Hello, world!

So what happens if we call a function and pass in more than one positional parameter . Of course, it will be an error.

Exmaple ("Hello, world!", "another string")

>> typeerror:exmaple () takes exactly 1 argument (2 given)
When you define a function, use *arg and **kwarg

*arg and **kwarg can help us deal with the above situation, allowing us to pass in multiple arguments when calling a function

def exmaple2 (Required_arg, *arg, **kwarg):
    if arg:
        print "arg:", arg

    if Kwarg:
        print "Kwarg:", kwarg< C12/>exmaple2 ("Hi", 1, 2, 3, keyword1 = "Bar", Keyword2 = "foo")

>> arg:  (1, 2, 3)
>> Kwarg:
  {' keyword2 ': ' foo ', ' keyword1 ': ' Bar '}

As you can see from the example above, when I pass in more arguments *arg will convert the extra positional parameters into tuple **kwarg will convert the keyword parameters to Dict

For example, an addition function that does not set the number of parameters

def sum (*arg):
    res = 0 for
    e in arg:
        res + e return
    res

print sum (1, 2, 3, 4)
print sum (1, 1) 
  
   >>
>> 2
  

Of course, if you want to control the keyword parameters, you can use a single * as a special separator symbol. Limited to Python 3, the following example defines only two keyword parameters, and the parameters are named Keyword1 and Keyword2

def person (Required_arg, *, Keyword1, keyword2):
    print (Required_arg, keyword1, keyword2) person

("Hi", Keyword1= "Bar", keyword2= "foo")
>> Hi bar foo

If you do not pass in the parameter names Keyword1 and KEYWORD2 will make an error, because they will be considered positional parameters .

Person ("Hi", "Bar", "foo")

>> Typeerror:person () takes 1 positional argument but 3 were given
use *arg and **kwarg when calling functions

The direct example is very similar to the above. Reverse thinking.

def sum (A, B, c): return
    A + B + c

a = [1, 2, 3]

# the * unpack List a 
print sum (*a)
>> 6
def sum (A, B, c): return
    A + B + c

a = {' A ': 1, ' B ': 2, ' C ': 3}

# the * * Unpack dict a
print sum (**a)
>> 6


Author: Jason_yuan
Link: http://www.jianshu.com/p/e0d4705e8293
Source: Jianshu
Copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.
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.