Dict with `=` support similar to f-str `=`

Motivation

Please see the below snippet:

some_thing = 5
another_thing = 6

gets_unpacked_into_kwargs = {
    "some_thing": some_thing,
    "another_thing": another_thing
}

some_func(**gets_unpacked_into_kwargs)

In the above code, we see the author desires a 1-1 correspondence of the some_func’s kwarg names with variable names, so later dict unpacking can be directly applied. The kwarg names have been duplicated to be the dict keys.

I think this is somewhat common in facade interfaces and wrapper functions.

Idea

Python 3.8 introduced the = specifier to f-strings for debugging (docs link).

I think it could be interesting if dict supported something similar:

some_thing = 5
another_thing = 6

gets_unpacked_into_kwargs = {
    some_thing=,
    another_thing=
}

print(gets_unpacked_into_kwargs)
# {'some_thing': 5, 'another_thing': 6}

This would enable:

  • DRY facade/wrappers, because it decreases the retyping of names to enable dict unpacking downstream
  • Remove a degree of freedom from the facade/wrapper, as now the 1-1 correspondence has one less indirection (retyping kwarg names as dict keys)

Cheers to Python!

2 Likes

See previous discussion: Shorthand notation of dict literal and function call

2 Likes

Oh dang, I made a duplicate! I will read through the other thread, and anything valuable from this not mentioned there, I will post.

Thank you!