Python "Args" and "Kwargs" in User-defined Functions

Python "Args" and "Kwargs" in User-defined Functions

Understand how "Args and Kwargs" help other developers read your codes easily and style your codes better.

Just a quick question...Have you gone through articles, videos, books on creating user-defined functions before reading this article?

Why? It'll help you understand what I'm about to explain.

When creating user-defined functions in python, it's of great importance that you write your functions in a way other developers can easily understand your code.

So, "Args" and "Kwargs" are definitely going to help you as a developer. If you didn't understand this aspect while going through it, then keep your fingers crossed while I explain it to you.

Let's dive in!

Args in Python

"Args" is identified in a user-defined function with a single asterisk(*) as a parameter.

 def myfunc(*args):
     pass

The word "args" can be any word you like but it's advisable for it to remain as "args" so that other developers can understand your code likewise you in future.

Here's an example of a code to calculate the mean of 5 numbers below;

def mean(a,b,c,d,e,):
    '''returns the mean or average of 5 numbers'''
    return sum((a,b,c,d,e)) / len((a,b,c,d,e))

print(mean(5,2,6,8,7))

From the example given above you'll see the code is right but it's looks a bit messy.

What if you want to increase the number of arguments to 10? It means the number of parameters has to be adjusted also in your function.

Here's where "args" comes in to make your work tidier and easier.

def mean(*args):
    '''returns the mean or average of numbers'''
    return sum(args) / len(args)

print(mean(5,2,6,8,7,6,4,2,3,6))

From the above example, you can see that only the number of arguments was altered.

"args" in the code is a "tuple" of values and that's why we can also use the function "sum".

def add(*args):
    "Program calculates the sum of three numbers"
     print(type(args))
     return sum(args)

print(add(5,2,4))

Looping through the "args"

def num(*args):
    for numbers in args:
        print(numbers)
num(5,3,6,8)

Kwargs in Python

"Kwargs" has the same objective of styling your codes better when using user-defined functions in python. It's identified with a double asterisk(**) as a parameter.

Where "args" is a tuple of value, "kwargs" is a dictionary. Here's an example below;

def myfunc(**kwargs):
    print(type(kwargs))
    #dictionary of values

    if "fruit" in kwargs:
        print("My fruit of choice is {}".format(kwargs['fruit']))
    else:
        print('I did not find any fruit here')
        print("I found a {} instead".format(kwargs["vegetable"]))

myfunc(fruit='apple',vegetable='carrot')

Conclusion

"args" and "kwargs" are important aspects every python developer must understand to write your codes better and easy to understand to other developers.

When using your user-defined functions and there has to be multiple arguments, use "args" or "kwargs."