Problem
What distinguishes “keyword arguments” from other types of arguments? Isn’t it possible to pass all arguments as name=value instead of using positional syntax?
Asked by mk12
Solution #1
There are two concepts that are connected to each other, both of which are referred to as “keyword arguments.”
You have the opportunity to specify some function arguments by name on the calling side, as indicated by other commenters. You must mention them after all of the arguments without names (positional arguments), and any parameters that were not mentioned at all must have default values.
The other principle is in function definition: you can build a function that takes parameters by name, without even specifying what those names are. They can’t be passed positionally because they’re only keyword arguments. The syntax is as follows:
def my_function(arg1, arg2, **kwargs)
Any keyword arguments passed to this function will be stored in the kwargs dictionary. You can look at the keys of this dictionary in real time, for example:
def my_function(**kwargs):
print str(kwargs)
my_function(a=12, b="abc")
{'a': 12, 'b': 'abc'}
Answered by Ian Clelland
Solution #2
There is one last aspect of language where the distinction is critical. Think about the following function:
def foo(*positional, **keywords):
print "Positional:", positional
print "Keywords:", keywords
There is no limit to how many positional arguments you can send to foo() using the *positional argument.
>>> foo('one', 'two', 'three')
Positional: ('one', 'two', 'three')
Keywords: {}
Any keyword arguments will be stored in the **keywords argument:
>>> foo(a='one', b='two', c='three')
Positional: ()
Keywords: {'a': 'one', 'c': 'three', 'b': 'two'}
You can use both at the same time, of course:
>>> foo('one','two',c='three',d='four')
Positional: ('one', 'two')
Keywords: {'c': 'three', 'd': 'four'}
These features are rarely utilized, but when they are, it’s critical to understand which arguments are positional and which are keywords.
Answered by too much php
Solution #3
Keyword arguments are the same as regular arguments, except that the order doesn’t matter. The following two function calls, for example, are identical:
def foo(bar, baz):
pass
foo(1, 2)
foo(baz=2, bar=1)
Answered by Eli Grey
Solution #4
They don’t have any keywords in front of them. The sequence is crucial!
func(1,2,3, "foo")
They have keywords at the top of the page. They don’t have to be in that order!
func(foo="bar", baz=5, hello=123)
func(baz=5, foo="bar", hello=123)
It’s also worth noting that if you use default arguments and forget to include the keywords, the order will matter!
def func(foo=1, baz=2, hello=3): ...
func("bar", 5, 123)
Answered by Unknown
Solution #5
Both methods are used to assign argument values to function parameters.
It’s worth noting that positional arguments are an option.
If you don’t use positional arguments, everything you’ve written transforms into a keyword argument.
When calling a function, you must decide whether to utilize position, keyword, or a combination of both. If you want, you can do all of the keywords. Some of us do not make this choice and use positional arguments.
Answered by S.Lott
Post is based on https://stackoverflow.com/questions/1419046/normal-arguments-vs-keyword-arguments