fbpx

Unpacking Function Arguments in Python

Pythoniccode1

ARTICLE SUMMARY

Our blog squad member Indhumathy shares packing and unpacking function arguments in detail in Python.

In python functions, we can pack or unpack function arguments.

Unpacking: During function call, we can unpack python list/tuple/range/dict and pass it as separate arguments. * is used for unpacking positional arguments. ** is used for unpacking keyword arguments.

You're invited! Join SheCanCode's Women in Tech Community

Find a supportive network, opportunities, jobs & much more, so you can excel in your tech career.

 

Packing: You might have seen *args and **kwargs in a python function definition. What does that mean? It’s known as packing, which packs all the arguments in a tuple. If we don’t know the number of arguments to be passed during the function call, we can use packing.

Let’s learn about packing and unpacking function arguments in detail in this article.

Recap of the function definition:

Just a little recap of the function definition. While defining a function, we will specify formal parameters and during the function call, we will specify the actual parameters.

python

We can use arbitrary arguments during the function call or during the function definition. Arbitrary arguments can be positional or keyword arguments.

Four ways of unpacking/packing function arguments:

     

      • Unpacking positional arguments

      • Packing positional arguments

      • Unpacking keyword arguments

      • Packing keyword arguments

    Unpacking positional arguments

    When the arguments are in the form of sequences like list/tuple/range, we can unpack them during the function call using the * operator.

    python

     

    An asterisk * is used for unpacking positional arguments during the function call.

    positional arguments: Positional arguments means values passed through function call should be in the same order mentioned during the function definition.

    Example 1: Unpacking positional arguments during the function call.

    mul(*num )→ It will unpack the elements in the list and will assign them to the parameters mentioned in the function definition in the same order.

    1 → a
    2 →b
    3 →c

    python

    Example 2: Checking how the unpacking is done.

    python

    Example 3: TypeError raised when the number of parameters doesn’t match

    After unpacking arguments during the function call, if the number of arguments doesn’t match the parameters in the function definition, it will raise an error.

    python

    Example 4: If we just mention the list name during the function call, it will raise an error.

    Here, in this example, the entire list → color is assigned to parameter a. If we just mention the list name during the function call, it won’t unpack and assign the list elements to the parameters.

    python

    Packing positional arguments 

    Sometimes during the function call, we may use an arbitrary number of arguments. Since we don’t know the number of arguments to be passed during the function call, we can use packing to pack all the arguments in a tuple.

    python

    Example 1: Passing an arbitrary number of arguments during the function call. 

    The same function can be used for different numbers of parameters.

    python

    Example 2: Before the variable number of arguments, zero or more normal arguments can occur. 

    python

    Example 3: Error is raised when normal parameters appear after the variable number of arguments.

    The normal arguments should not appear after the variable number of arguments. Only keyword arguments should occur after the variable number of arguments.

    python

    Unpacking keyword arguments

    When the arguments are in the form of a dictionary, we can unpack them during the function call using the ** operator.

    python

    A double asterisk ** is used for unpacking a dictionary and passing it as keyword arguments during the function call.

    Keyword arguments: keyword arguments are in the form kwargs=value

    Example 1: Unpacking dictionary during the function call.

    mul(**d )→ It will unpack the elements in the dictionary and pass it as keyword arguments during the function call.

    a=4
    b=5
    c=7

    python

    Packing keyword arguments

    Sometimes during the function call, we may use an arbitrary number of keyword arguments. Since we don’t know the number of keyword arguments to be passed during the function call, we can use packing to pack all the keyword arguments in a dictionary.

    python

    Example 1: Passing an arbitrary number of keyword arguments during the function call.

    The same function can be used for different numbers of parameters.

    Conclusion:

    Unpacking function arguments are used when we want to unpack list/tuple/dict during the function call.

    Packing function arguments are used when we don’t the number of parameters passed during the function call. The same function can be used for the different number of parameters.

    RELATED ARTICLES

    Join SheCanCode for a day of ideation and coding at our Financial Inclusion Power Hack! Spend the day coding solutions that will help tackle financial...
    Join SheCanCode for a day of ideation and coding at our International Women’s Day Power Hack! Spend the day coding solutions that will directly help...
    The article delves into the extensive capabilities of NumPy, a critical numerical computing library in Python. It provides a comprehensive NumPy cheat sheet, covering essential...
    The article provides a comprehensive dictionary of common coding languages, offering insights into their key features and diverse applications. From Python's versatility to Swift's role...

    This website stores cookies on your computer. These cookies are used to improve your website and provide more personalized services to you, both on this website and through other media. To find out more about the cookies we use, see our Privacy Policy.