Understanding args And kwargs In Python
Tags: Best Practices, Programming, Python3, Python, Python Programming

If you are pythonic or pythonista or another call for a python programmer, args and kwargs is one of the mandatory python features should you know. Both keywords almost used in every method or function definition.

Args keyword

Python has three famous data structure data type i.e. list tuple and dictionary . Python has another type, but the famous is three before. Args keyword is the magic parameter that contains more that one data. If you are familiar with varags Java or Kotlin, the keyword is the same. Args keyword is the tuple type parameter but extracted one by one. Let’s see the following sample args definition.

Result: <class 'tuple'>

According to the code above, the args type is tuple which means the parameter able to receive one more value in parameter. For example, let’s see the sample below.

Result: The first args parameter is : 1

(1,2,3,4,5)

We have the_magic_of_python function that only have 2 common parameters args and kwargs . In the sample above, the function only receives value for args the parameter. Cause args is tuple we can take the value of the parameter by index. If the index does not provide data, the system will throw the IndexError. Follow another sample below

Result: This is the 0 parameter, value True

This is the 1 parameter, value False

This is the 2 parameter, value 1

This is the 3 parameter, value 2

This is the 4 parameter, value 3

Easy right? . . .

The type of args is tuple , different case with kwargs . Kwargs stands for Keyworded Argument which means the function parameter with keyword definition to identify specific parameter. Kwargs is dictionary with key and value-based arguments. Let’s see the sample below

Result: <cls 'dict'>

Another kwargs sample

Result: The first value of kwargs is : 15

{'first': 15, 'second': 26}

According to the sample above, the value of the kwargs only the parameter with keyword definition i.e. first and second.

Let’s see the following detail

Result: This is args values : (True, False)

This is kargs values : {'first': 15, 'second': 26}

When we use args and kwargs as a parameter

In the common case, args and kwargs will be used if the parameter of the function is too much. The args and kwargs keyword will reduce the parameter definition in the function and able to receive dynamic parameter given.

def this_is_sample(first, second, third, fourth, fifth, sixth, seventh=None, eighth=None):

print(first, second, third, fourth, fifth, sixth) this_is_sample(1,2,3,4,5,6,7,8)

You should use this code instead

def this_is_sample(first, second, third, *args):

print(first, second, third, *args) this_is_sample(1,2,3,4,5,6,7,8,8,10,11,12,13,14,15,16,17,18)

Final Sample

In the final sample, the function only has args and kwargs parameter. The args parameter will be defined as a tuple and set into args_params variable. The kwargs parameter will define as dictionary and set into kwargs_params variable.

Result: Oh nice, this time to try python

Conclusion

Args will be used to extract value from the ordered value given into the parameter. If the function has 3 params with a,b,c . then we give 3 value *(1,2,3), args will extract the value sequenly like a=1 b=2 c=3 . Different from kwargs, this parameter able to extract value from the ordered and unordered parameters. Either parameter will be placed depending on the key definition.

👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬