Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 1.33 KB

11_star_star_operator.md

File metadata and controls

40 lines (33 loc) · 1.33 KB

** works like * but for named arguments.

When you define a function, ** means to take all the named arguments which weren't matched, and put them into a dict.

def do_something_with_a_dict(**kwargs): ... print len(kwargs) ... print kwargs.class ... for k in kwargs: ... print k, kwargs[k]

do_something_with_a_dict(a=1, b=2) 2 <type 'dict'> a 1 b 2

(kwargs is just the common name used for the dictionary argument. Any name can be used.)

When you call a function, ** means to take a dict, and expand it into individual named variables.

def what_they_ate(mummy, daddy, baby): ... print "Mummy bear's porridge was too %s." % mummy ... print "Daddy bear's porridge was too %s." % daddy ... print "Baby bear's porridge was just %s." % baby

d = {'daddy': 'cold', 'mummy': 'hot', 'baby': 'right'} what_they_ate(**d) Mummy bear's porridge was too hot. Daddy bear's porridge was too cold. Baby bear's porridge was just right.

d = {'daddy': 'cold', 'mummy': 'hot'} what_they_ate(**d) Traceback (most recent call last): ... TypeError: what_they_ate() takes exactly 3 arguments (2 given)

d = {'daddy': 'cold', 'mummy': 'hot', 'baby': 'right', 'goldilocks': 'cheeky'} what_they_ate(**d) Traceback (most recent call last): ... TypeError: what_they_ate() got an unexpected keyword argument 'goldilocks'