Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kwargs reserved word in python. What does it mean? [duplicate]

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

like image 634
boB Avatar asked Nov 23 '13 04:11

boB


People also ask

How does Kwargs work in Python?

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).

How do you escape a keyword in Python?

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.

Can you have multiple Kwargs in Python?

Python 3.5+ allows passing multiple sets of keyword arguments ("kwargs") to a function within a single call, using the `"**"` syntax.

How do you pass Kwargs in Python?

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) .


2 Answers

**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}
like image 115
thefourtheye Avatar answered Sep 18 '22 05:09

thefourtheye


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.

like image 45
Gabe Avatar answered Sep 20 '22 05:09

Gabe