1

I recently came across a double * and was curious if it was special to the sample code I was using or a special operator in Python.

Example Code:

current_page = reports_service.activities().list(**params).execute()

params is a dictionary in this example.

The code in question is a segment from Google's reports API

Dan O'Boyle
  • 3,676
  • 5
  • 28
  • 44
  • It is an operator in Python http://stackoverflow.com/questions/5710391/converting-python-dict-to-kwargs – igon Oct 30 '14 at 22:36
  • The duplicate responds to my question, but I don't think it does so directly or as cleanly as mine. – Dan O'Boyle Oct 30 '14 at 22:40
  • 1
    If `params` is a dict like `{"a":1, "b":2, "c":"spam"}`, then `list(**params)` is equivalent to `list(a=1, b=2, c="spam)`. The ** is essentially expanding the dict into keyword-arguments. When you see `**` in a function definition like `def thing(**kwargs)`, it is doing the opposite - combining keyword arguments given to the function into a dict that can be used within the function. – Tom Dalton Oct 30 '14 at 23:03
  • @TomDalton, to clarify, a `**` parameter in a function definition doesn't collect all keyword arguments, just those that don't map to named parameters. It also doesn't include Python 3 keyword-only parameters. – Eryk Sun Oct 31 '14 at 01:46

0 Answers0