Snargs : triple-star packing/unpacking of self-named arguments (and alternative to PEP 736)

Also, this seems like a lot of change with fairly little value, while the main issue can be addressed by introducing something similar to json object constructor:

const a = 'foo';
const b = 42;
const c = {};
const object3 = { a, b, c };
console.log(object3);
// Object { a: "foo", b: 42, c: Object {  } }

Of course, this is set’s definition so different syntax is needed, e.g.:

a, b, d = 1, 2, 4
d = {{a, b, 'c': 3, d}}
print(d)    # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# With or without possibility to mix in usual syntax

I believe this has been discussed in PEP 736: Shorthand syntax for keyword arguments at invocation and in other places, which I can not find now (there definitely were discussions about this specifically).

If this was done at dict level (as opposed to function call syntax), it would have wider benefits, while also providing shorthand for function calls. e.g.:

def foo(a, b, c):
    pass

a, b, c = 1, 2, 3
foo(**{{a, b, c}})

Maybe not as convenient as foo(a=,b=,c=), but not that much longer, while this way it has much more value with much less effort.

  1. Less effort:

    • Adding additional dict constructor syntax I would guess is a bit easier than function call modifications. At least from what I have seen, function argument logic is already quite involved.
    • PEP736 rejection reason1: “… complicating the grammar around function arguments …”
  2. More value:

    • Addresses general dict construction and not only function keywords.
    • PEP736 rejection reason2: “… new syntax does not have a high enough payoff to justify the added complexities …”

So yes, it wouldn’t be able to do this:

But:

  1. How often is this needed?
  2. This is quite a lot of dev work for replacing 2 lines with 1:
a, b, c = ***snargs
# vs
assert set(snargs) == {'a', 'b', 'c'}
a, b, c = snargs.values()

I think adapting JSs dict constructor variant is pretty much the best bet to address this, which from what I have seen is the main motivator for this direction in the first place.

4 Likes