[Python-ideas] Syntax for allowing extra keys when unpacking a dict as keyword arguments

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Apr 12 19:08:56 EDT 2019

Bruce Leban wrote:
> I think this is a real problem given the frequent convention that you 
> can freely add fields to json objects with the additional fields to be 
> ignored.

Unpacking json objects isn't something one does every day, so I
don't think it would be worth adding syntax just for this.

Rather than new syntax, how about a helper function such as:

def call_with_kwds_from(func, kwds):
     code = func.__code__
     names = code.co_varnames[:code.co_argcount]
     func(**{name : kwds[name] for name in names})

Usage example:

def f(a, b):
     print("a =", a, "b =", b)

d = {"a": "a_value", "b": "b_value", "c": "something extra"}

call_with_kwds_from(f, d)


More information about the Python-ideas mailing list