Rules of arg, kwarg unpacking

samuelbrody1249 :

What are all the places that (kw)arg unpacking can be used?

For example:

>>> a={1:2}
>>> b={3:4}

# ok
>>> {**a,**b}
{1: 2, 3: 4}

# ok
>>> [*a]
>>> [1]

# error
>>> (*a)
SyntaxError: can't use starred expression here

What are all the 'rules' that starred-expressions abide by?

blhsing :

Unpacking works in a tuple too. However, a single-item tuple (also known as a singleton) has to include a comma at the end of the expression enclosed in the parentheses, or else the parentheses would be treated as simple grouping, so do instead:

(*a,)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=5301&siteId=1