There are two related concepts, both called "keyword arguments".
On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which were not mentioned at all.
The other concept is on the function definition side: you can define a function that takes parameters by name -- and you don't even have to specify what those names are. These are pure keyword arguments, and can't be passed positionally. The syntax is
def my_function(arg1, arg2, **kwargs)
Any keyword arguments you pass into this function will be placed into a dictionary named kwargs. You can examine the keys of this dictionary at run-time, like this:
def my_function(**kwargs):
print str(kwargs)
my_function(a=12, b="abc")
{'a': 12, 'b': 'abc'}
Answer from Ian Clelland on Stack OverflowVideos
There are two related concepts, both called "keyword arguments".
On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which were not mentioned at all.
The other concept is on the function definition side: you can define a function that takes parameters by name -- and you don't even have to specify what those names are. These are pure keyword arguments, and can't be passed positionally. The syntax is
def my_function(arg1, arg2, **kwargs)
Any keyword arguments you pass into this function will be placed into a dictionary named kwargs. You can examine the keys of this dictionary at run-time, like this:
def my_function(**kwargs):
print str(kwargs)
my_function(a=12, b="abc")
{'a': 12, 'b': 'abc'}
There is one last language feature where the distinction is important. Consider the following function:
def foo(*positional, **keywords):
print "Positional:", positional
print "Keywords:", keywords
The *positional argument will store all of the positional arguments passed to foo(), with no limit to how many you can provide.
>>> foo('one', 'two', 'three')
Positional: ('one', 'two', 'three')
Keywords: {}
The **keywords argument will store any keyword arguments:
>>> foo(a='one', b='two', c='three')
Positional: ()
Keywords: {'a': 'one', 'c': 'three', 'b': 'two'}
And of course, you can use both at the same time:
>>> foo('one','two',c='three',d='four')
Positional: ('one', 'two')
Keywords: {'c': 'three', 'd': 'four'}
These features are rarely used, but occasionally they are very useful, and it's important to know which arguments are positional or keywords.
Yesterday, I was going through the O Reilly book on Python and was trying to understand more about Functional programming. usually, I just pass positional arguments(instinctively). I hardly try to use keyword arguments or kwargs.
Apparently, in other languages like C or C++, you have to pass arguments in a particular order. But in Python, you can overcome this by using keyword arguments.
def my_func(a, b, c):
return a + b + c
print(my_func(a=4, c=2, b=5))
Here, we are able to change the order of our arguments by using keyword arguments.
The thing to be careful in Python is when you mix positional and keyword arguments.
def my_func(a, b, c):
return a + b + c
print(my_func(4, c=2, 5))
You cannot do the above! Python will not explicitly assume that 5 refers to b. Once you start using keyword arguments, you cannot use positional arguments anymore. So you have to write like this.
def my_func(a, b, c):
return a + b + c
print(my_func(4, c=2, b=5))
I wrote my learning about different types of arguments like - positional, *args and **kwargs in my blog. Please check it out if you would like to read it.