unpack operator in Python

Updated post here: https://codeyarns.github.io/tech/2012-04-25-unpack-operator-in-python.html

6 thoughts on “unpack operator in Python

  1. Thanks! Clear explanation. But why doesn’t the unpack operator work outside of function definitions? Why can’t I say l=[1,2,3] and then the expression *l == 1 2 3 (individually, unpacked). Is this the only example of python syntax that is unique to function definitions? [would it even be useful outside of that context? Has this been discussed? Just curious]

    Liked by 2 people

    1. Slehar: I have no idea why it is enabled only for function arguments. Probably because elsewhere you can do without it. But a function is defined once, but is called from multiple locations. Some location may have a list, some other location might have individual variables. You can now call the function from both places with least effort.

      Like

  2. Useful post, but the output for the last example is wrong: The output should properly match the keyword args as

    foo(**d)
    (‘First is ‘, 15, ‘ then ‘, 23, ‘ lastly ‘, 56)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.