I am using Python trying to figure out a key word and I see the word, "kwargs
", which I know is some kind of argument in the called function but I can not find what it means or stands for anywhere.
For example, this entry in the Python documents says...
read_holding_registers(address, count=1, **kwargs)
address – The starting address to read from
count – The number of registers to read
unit – The slave unit this request is targeting
It looks like a reference to a pointer to pointer but that is all I can tell...
This does NOT even use "**kwargs
" in the parameters list It uses
what looks to me like, "unit
" instead of "kwargs
".
I can NOT seem to find anywhere what kwargs
means.
Maybe it is "Key Word arguments" ? What am I missing here ?
Any ideas on help here ? thank you ! boB
The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is that the double star allows us to pass through keyword arguments (and any number of them).
To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.
Python 3.5+ allows passing multiple sets of keyword arguments ("kwargs") to a function within a single call, using the `"**"` syntax.
The ** unpacking operator can be used to pass kwargs from one function to another function's kwargs. Consider this code: (newlines don't seem to be allowed in comments) def a(**kw): print(kw) , and def b(**kw): a(kw) .
**kwargs
means keyword arguments. Its actually not a python keyword, its just a convention, that people follow. That will get all the key-value parameters passed to the function. For example,
def func(*args, **kwargs):
print args, kwargs
func(1, "Welcome", name="thefourtheye", year=2013)
will print
(1, 'Welcome') {'name': 'thefourtheye', 'year': 2013}
Yes, it means "keyword arguments", but kwargs
is not actually a reserved word. It is just the idiomatic thing to call the argument that collects all the unused keyword arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With