what does **kw mean?

Lie Lie.1296 at gmail.com
Fri Jan 11 12:24:01 CET 2008


On Jan 11, 4:38 pm, "zsl... at gmail.com" <levili... at gmail.com> wrote:
> I've been reading the following example, and couldn't figure out, what
> **kw mean. (It's an empty dictionary, but what's the semantics):

It's a keyword argument. It's some kind of repository for arguments
that aren't recognized.

If you have function like this:
def func(a, *args, *kw):
    print a
    print args
    print kw

and you call the functin like this:
func('value A', 'value B', 'value C', argumentA = 'value D', argumentB
= 'value D')
the extra arguments would normally raise an error, but with the * and
**, Python would:
- assign 'value B' and 'value C' to args
- assign 'argumentA':'value D' and 'argumentB':'value E' to kw

so if you run the function, it will output:
####
value A
('value B', 'value C')
{'argumentB': 'value E', 'argumentA': 'value D'}
####

this args and kw can be accessed like a tuple and dictionary
respectively

See '4.7.2 Keyword Arguments' and '4.7.3 Arbitrary Argument Lists' on
Python Help File



More information about the Python-list mailing list