Asterisks in Python function: Unpacking & beating

Source: Internet
Author: User
Tags unpack

The use of ' * ' and ' * * ' in Python is divided into two aspects, one is the computation and the other is the packaging and unpacking of elements in the process of parameter passing.

Computational aspects

The most common functions of ' * ' and ' * * ' in Python are ' multiply ' and ' be ' multiplied ', as follows:

>>> a  = 2>>> b = 3>>> c = 5*6>>> d = 4**3>>> E = A *b>>> f = a**b
Variable parameters for passing, packaging, and unpacking aspects of functions
    • The parameters passed by the function are packaged (Narimoto group tuple or Dictionary dictionary) and disassembled (decomposed into a single element), where the tuple's packaging and unpacking uses the single number ' * ', and the dictionary is packaged and disassembled using ' * * '.
    • packaging , is to pass to the function of any number of (or can be 0) non-keyword parameter/keyword parameter package into a tuple/dictionary (tuples can only receive non-keyword parameters, the dictionary can only receive keyword parameters)

When a function has an asterisk * in front of its arguments, this is a variable positional parameter, and a two asterisk * * indicates a variable keyword parameter.

defFoo (*args, * *kwarg): forIteminchargs:PrintItem forKvinchKwarg.items ():Printk,vPrint30*'='if __name__=='__main__': foo (1, 2, 3, a=4, b=5) foo (2, 3, a=4, b=5, c=1)

The output is as follows:

" C:/python s8/develops/day 2 control statement/asterisk usage. PY "12345**************************************************2345  1**************************************************Process finished with exit code 0

So we can pass in any number of arguments.

Unpack/scatter (unpack) parameters
    • Disassembly is a list, a tuple, or a dictionary that is passed to a function is split into separate elements and then assigned to the parameter variables in the function (including the normal positional parameters, keyword parameters, tuples are also * non-keyword parameters, the dictionary is the * * keyword parameter).
    • In the solution of the dictionary there will be two solutions, one using the * solution, the solution to the function of only the key value (. Key) The other is to use * * solution, the solution is a dictionary of each.

Asterisk * Unpack the sequence/set (unpack) into positional parameters, two asterisk * * Unpack the dictionary into keyword parameters .

Here's an example to further deepen your understanding:

defFoo (*args, * *kwarg): forIteminchargs:Print(item) forKvinchKwarg.items ():Print(k, v)Print('*'* 50)if __name__=='__main__':    #foo (1, 2, 3, a=4, b=5)    #foo (2, 3, a=4, b=5, c=1)v = (1, 2, 4) V2= [11, 15, 23] D= {'a': 1,'b': 12} foo (v, D) Foo (*V, * *d) Foo (v2, D) Foo (*v2, **d)

The output is as follows:

C:\Users\BigTree\AppData\Local\Programs\Python\Python36\python.exe"C:/python s8/develops/day 2 control statement/asterisk usage. PY"(1, 2, 4){'a': 1,'b': 12}124a1b12**************************************************[11, 15, 23]{'a': 1,'b': 12}111523a1b12**************************************************Process finished with exit code 0

In the example above v、v2、d , if there is no asterisking number then it is passed to the function as an argument, and if an asterisk is added, it will be unpacked and passed to the function.

foo(*d, **d)Equivalent to foo(1, 2, 4, a=1, b=12) .

A few notes.

The variable positional parameter *args is a tuple and is not modifiable.

>>>defFoo (*args): ... args[0]= 5... >>> foo (1, 2, 3) Traceback (most recent): File"<stdin>", Line 1,inch<module>File"<stdin>", Line 2,inchFootypeerror:'tuple'Object does notSupport Item Assignment>>> L = [1, 2, 3]>>> Foo (*l) Traceback (most recent): File"<stdin>", Line 1,inch<module>File"<stdin>", Line 2,inchFootypeerror:'tuple'Object does notSupport Item Assignment

No matter how we pass in the parameter, args it is a tuple type and cannot be modified.
For dictionary types, if you use only one model * then only the dictionary key is passed in.

>>>defFoo2 (*args, * *kwarg): ...Printargs, Kwarg ...>>> d = {'a': 1,'b': 2,'C': 3}>>> Foo2 (*d) ('a','C','b') {}>>> Foo2 (* *d) () {'a': 1,'C': 3,'b': 2}

参考链接:http://blog.csdn.net/xiaoqu001/article/details/78823498




Asterisks in Python function: Unpacking & beating

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.