9

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

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
boB
  • 91
  • 1
  • 4

2 Answers2

14

**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}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Thank you. I thought it might be a general purpose args placeholder but was not sure. Why the double asterisks ? It makes me think it is an indexed operation of some sort... Pointer to a pointer. I am still unsure here. – boB Nov 23 '13 at 05:40
  • @user3024070 Its by the syntax of the language. There are no pointers in Python. Check this question out http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters It has complete explanation of this – thefourtheye Nov 23 '13 at 05:44
8

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.

Gabe
  • 84,912
  • 12
  • 139
  • 238