sub-title

Also check Orama's Quora and Orama's GitHub
I shall not claim to know so much, but only that I learn new things everyday

Thursday, 17 March 2022

Demystifying args and kwargs in Python

If you use Python for your data science or software development projects (or for amything else), then you have most likely used or seen args and kwargs. They may look intimidating at first, but they are easy to understand and use. 

To understand args and kwargs, let us start by defining a simple function

def print_str(str1):
	print(str1)

The above function takes one “required” parameter called str1, hence you can call

print_str(“my string”)

You cannot call

print_str()

because one required argument must be passed in the function call. The above example is a trivia.

What if you wanted to pass another one or two or more arguments which are optional. Then you can use args and/or kwargs. Both provide a handy and flexible way to handle optional and variable number parameters in functions. They have many practical use cases. 

Please note the difference between parameters and arguments: parameters are used in function definitions, while arguments are used in function calls. Sometimes, parameters are referred to as Formal arguments, while arguments are referred to as Actual arguments, but don't allow this to confuse you.

In the above example, def print_str(str1) means that str1 is a parameter since it is being used in a function definition, while print_str(“my string”) means that “my string” is an argument since it is being used in a function call. I hope that clarifies a common confusion. Now let's move on to args and kwargs.


args (stands for arguments)

In the following example, a corresponding function call must supply one required argument (str1) and a variable number of positional arguments (args).

def print_str(str1, *args):
	print(str1)

	for s in args:
    	print(s)

 

Note the following:

  1. args must have the unpacking operator ‘*’ before it.
  2. args could be named anything, provided ‘*’ appears before it. We just prefer to name it args by convention.
  3. args is a collector of optional positional arguments. It collects the arguments and packs them into a tuple, which is an iterable.
  4. args works for positional arguments.

Now, you can call the function by passing as many “args” as you want. Note that args are also positional arguments, just like the required arguments.

        print_str(‘my string’, ‘arg1’ , ‘arg2’, ‘arg3’, ‘arg4’)

        print_str(‘my string’, ‘arg1’ , ‘arg2’)

 

kwargs (stands for keyword arguments)

In the following example, a corresponding function call must supply one required argument (str1), a variable number of positional arguments (args), and a variable number of keyword arguments (kwargs).

def print_str(str1, *args, **kwargs):
	print(str1)
    
	for s in args:
    	print(s)

	for k,v in kwargs.items():
    	print(k,v)

Note the following:

  1. kwargs must have the unpacking operator ‘**’ before it.
  2. kwargs could be named anything, provided ‘**’ appears before it. We just prefer to name it kwargs by convention.
  3. kwargs is a collector of keyword arguments.  It collects the arguments and packs them into a dictionary, which is an iterable.
  4. kwargs works for named arguments.

Now, you can call the function by passing as many “kwargs” as you want. Note that kwargs are NOT positional arguments.

        print_str(‘my string’, key1=‘kwargs1’ , key2=‘kwargs2’, key3=‘kwargs3’)

        print_str(‘my string’, key1=‘kwargs1’)

Before I sign off, it is important to note that you must have good understanding of functions, data structures (specifically tuples and dictionaries) in order to effectively use args and kwargs.

I hope that makes it easier now. Your feedback is welcome.

No comments:

Post a Comment