On the 18th day of the November Gwen Challenge, check out the details of the event: the last Gwen Challenge 2021

Inside the English website come from the python developers guide PEP 448 – Additional Unpacking Generalizations | Python.org

This article mainly introduces * and ** to launch a detailed introduction.

Unpacking is proposed to be allowed inside tuple, list, set, and dictionary displays.

Unpacking operations can be applied to tuples, lists, collections, and dictionaries.

  • *args: argument Is used for lists, tuples, and sets
  • **kwargs: kwargument is used in dictionaries

I’ll start with a review of range and its use to generate tuples, lists, and collections.

li = list(range(7))
print(li)
tu = tuple(range(7))
print(tu)
se = set(range(7))
print(se)
Copy the code
[0, 1, 2, 3, 4, 5, 6]
(0, 1, 2, 3, 4, 5, 6)
{0, 1, 2, 3, 4, 5, 6}
Copy the code

Available position

* and ** can be used to unpack anywhere.

Specifically, in function calls, in comprehensions and generator expressions, and in displays.

  • Function calls: function calls
  • Comprehensions and generator expressions are introduced
  • -Chuck: Well, you’ll never tell me anything displays.

display

The so-called for display I think is direct output, (total can not be direct output 😶).

li = list(range(7))
tu = tuple(range(7))
se = set(range(7))

print(*li,*tu,*se)

Copy the code
1 2 3 4 5 6 0 1 2 3 4Copy the code

Unpack lists, tuples, and collections directly.

Print (**{“name”:” Sian “,”name”:25}) SyntaxError: invalid syntax

Although not used for direct output, it can be sliced.

dic = {"name":"sian"."name2":"sian"}
a = {'a':0,**dic,'c':1}

print(a)
Copy the code
{'a':0,"name":"sian","name2":"sian",'c':1}
Copy the code

** can be sliced, so can * :

li = [1.2.3]
a = [1,*li,0]

print(a)

Copy the code
[1, 1, 2, 3, 0]
Copy the code

Comprehensions and generator expressions

The generator derivation is:

print(i for i in range(7))
Copy the code
<generator object <genexpr> at 0x000002502D43A510>
Copy the code

The output tells you that this is a generator object.

You can create lists and collections with generator objects, but you cannot create tuples directly.

li = [i for i in range(7)]
se = {i for i in range(7)}
print(li,se)
Copy the code
[0, 1, 2, 3, 4, 5, 6] {0, 1, 2, 3, 4, 5, 6}
Copy the code

Unpacking generator objects with * is:

print(*[i for i in range(7)])
Copy the code
0, 1, 2, 3, 4, 5, 6Copy the code

Just omit variable names :clap:.

Function call

It’s very common for function calls.

def function(num,*args) :
    print(num)
    print(args)
    print(*args)
    
function(1.2.3.4.5.6.7)
function('a'.1.2.3.'b')
Copy the code
1
(2, 3, 4, 5, 6, 7)
2 3 4 5 6 7
a
(1, 2, 3, 'b')
1 2 3 b
Copy the code

* ARGS is used to receive arguments of arbitrary length and pack them up and store them in tuples.

But the position is not flexible enough, it will take all the arguments from the *args starting position to the end.

  • Def function(first,*args,last): function() missing 1 required keyword-only argument: ‘last’

    But you can manually specify parameters to solve this problem. For example, you force a value for last like this.

    def function(first,*args,last="c") :
        print("first:",first)
        print(args)
        print("last:",last)
    
    function(1.2.3.4.5.6.7)
    Copy the code
    first: 1
    (2, 3, 4, 5, 6, 7)
    last: c
    Copy the code
  • * Parameters before args cannot be specified manually.

    def function(first='a',*args) :
        print("first:",first)
        print(args)
        
    function(1.2.3.4.5.6.7)
    Copy the code
    first: 1
    (2, 3, 4, 5, 6, 7)
    Copy the code

Take a look at the ** receiving argument and package it as a dictionary.

def function(first='a',**kwargs) :
    print("first:",first)
    print(kwargs)
    
function(1,name = "Sian", age = 25,b=2)
Copy the code
first: 1
{'name': 'Sian', 'age': 25, 'b': 2}
Copy the code

The position of the argument list is the same as that of *. SyntaxError: invalid syntax. But the preceding arguments can be specified.