August 2018
Intermediate to advanced
366 pages
10h 14m
English
Since Python 3.5 through PEP 448, it's now possible to unpack multiple mappings to provide keyword arguments:
>>> def f(a, b, c, d): ... print (a, b, c, d) ... >>> d1 = dict(a=5, b=6) >>> d2 = dict(c=7, d=8) >>> f(**d1, **d2) 5 6 7 8
This solution is very convenient, but has two limits:
If you don't know where the mappings/dictionaries you are unpacking come from, it's easy to end up with the issue of duplicated arguments:
>>> d1 = dict(a=5, b=6) >>> d2 = dict(b=7, c=8, d=9) >>> f(**d1, **d2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() got multiple values for keyword argument 'b'
In the previous example, the b