kwargs reserved word in python. What does it mean?

boB picture boB · Nov 23, 2013 · Viewed 7.2k times · Source

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)

Parameters:

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

Answer

thefourtheye picture thefourtheye · Nov 23, 2013

**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}