Packing and Unpacking Arguments in Python

Photo by Chris Ried on Unsplash

Intro

The concept of List Destructuring (aka Packing and Unpacking) is pretty nifty. You must have come across codes containing functions with these *args and **kwargs as arguments. It always used to boggle my mind, what kind of arguments are these. After some research, I got to know how wonderful they are.

Let’s understand them one by one. Open your favorite code editor and just follow along.

Unpacking

Consider a function that takes 3 arguments and we have a list of size 3 with us that has all arguments for the function. If we simply pass a list to the function, the call doesn’t work. It’ll give you an error as shown below.

# a function that takes 3 arguments
def fun(a,b,c):
print('{} {} {}'.format(a, b, c))
# list of arguments
a = ['Tom', '&', 'Jerry']
# passing the list
fun(a)

The output would be something like this:

Error: TypeError: fun() missing 2 required positional arguments: ‘b’ and ‘c’

Now, unpacking comes into play. We can use ∗(single asterisk) to unpack the list of arguments as shown below.

# a function that takes 3 argumentsdef fun(a,b,c):print('{} {} {}'.format(a, b, c))# list of arguments
myList = ['Tom', '&', 'Jerry']
# unpacking the list into 3 arguments
fun(*myList)

Output:

Tom & Jerry

Packing

Now, consider a situation when we don’t know how many arguments need to be passed to a python function, we can use Packing to pack all arguments in a tuple and then use it according to our convenience.

# a function that uses packing to print unknown number of arguments
def friends(*names):
for name in names:
print(name)
friends('Tom')
friends('Tom', 'Jerry')

Output:

Tom

Tom

Jerry

Similarly, you can pack or unpack a dictionary of values by simply replacing ∗(single asterisk) with ∗∗(double asterisk).

Conclusion

The concepts of packing & unpacking are pretty useful for Pythonistas. Some common use cases are as follows:

  • Used in socket programming to send an infinite no. of requests to the server.
  • Used in the Django framework to send variable arguments to view functions.
  • There are wrapper functions that require us to pass in variable arguments.
  • Modification of arguments becomes easy but at the same time validation is not proper so they must be used with care.

Hope the article would have been helpful to you.

See you in the next one :-)

--

--

--

Hello World! I’m a Software Engineer. Loves toying the underlying structures of technologies in the world of zero’s and one’s.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Master Pinot & Kevin the Minion

A different point of view between technology and “root” engineering is growing — and it needs to be…

Rich text editor, sorting users, and improved accessibility

『HK電影』Tom & Jerry線上看小鴨完整版【2021完整版】~高清电影

5 AWS Tips and Tricks to Solidify your EC2 and RDS RI Planning in 2017

How to Encrypt Password for Privacy Protection in Python

Revisit Your Processes — Eliminate Redundancies

Instantiate and Destroying game objects in Unity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhinav Anand

Abhinav Anand

Hello World! I’m a Software Engineer. Loves toying the underlying structures of technologies in the world of zero’s and one’s.

More from Medium

For loop example with explanation using Java

ORM: The deepening division of labor

Utilizing Java in PySpark

Functional** Data Structures