Fix Python – What do *args and **kwargs mean? [duplicate]


Asked By – UberJumper

What exactly do *args and **kwargs mean?

According to the Python documentation, from what it seems, it passes in a tuple of arguments.

def foo(hello, *args):

    for each in args:

if __name__ == '__main__':
    foo("LOVE", ["lol", "lololol"])

This prints out:

['lol', 'lololol']

How do you effectively use them?

Now we will see solution for issue: What do *args and **kwargs mean? [duplicate]


Putting *args and/or **kwargs as the last items in your function definition’s argument list allows that function to accept an arbitrary number of arguments and/or keyword arguments.

For example, if you wanted to write a function that returned the sum of all its arguments, no matter how many you supply, you could write it like this:

def my_sum(*args):
    return sum(args)

It’s probably more commonly used in object-oriented programming, when you’re overriding a function, and want to call the original function with whatever arguments the user passes in.

You don’t actually have to call them args and kwargs, that’s just a convention. It’s the * and ** that do the magic.

The official Python documentation has a more in-depth look.

This question is answered By – Paul D. Waite

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0