Search code examples
pythonpython-3.xkeyword-argument

Writing a function using (**kwargs) then summing


I want to define a function. This function should be able to accept variable number of keyword arguments of not predefined names. Then the function returns the number of keyword arguments.

For example:

result = all_the_kwargs(my_kwarg = "random", second_kwarg = "more", some_number = 1)

print(result)  # should print the number 3

I don't understand how I can count the number of keyword arguments with heterogeneous values (integers and strings mixed).


Solution

  • def all_the_kwargs(**kwargs):
        return len(kwargs)
    

    This will take all the arguments as a dictionary and return the length of it.