2023年8月8日 星期二

python star symbol is unpacking

# explain

This PEP proposes extended usages of the * iterable unpacking operator and ** dictionary unpacking operators to allow unpacking in more positions, an arbitrary number of times, and in additional circumstances. Specifically, in function calls, in comprehensions and generator expressions, and in displays.


https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/

https://peps.python.org/pep-0448/#id6

 

# example 

print( [i for i in [*[1,2], 1]] )

# [1, 2, 1]

print( [i for i in [[1,2], 1]] )

# [[1, 2], 1]


def hello(*attrs):

    for s in attrs:

        print(s)

        

        

hello([1,2,3])

#[1, 2, 3]


hello(*[1,2,3])

#1

#2

#3

hello(1,2,3)

#1

#2

#3