I thought about it a lot recently. Specifically on your proposal, and in
general. Unpacking expression can have a much more uniform treatment in the
language, as an expression with special "bare tuple" type - like tuple, but
"without the braces".

It also gives mental explanation for the conditional expression, where "a
if cond" is an unpack expression whose value is "*[a]" if cond hold, and
"*[]" otherwise. without context, this is an error. But with an else, "a if
cond else b" becomes "*[] else b" which evaluates to b. The result is
exactly like today, but gives the ability to build conditional elements in
a list literal:

x = [foo(), bar() if cond, goo()]
y = [1, bar()?, 3]

x is a list of 2 elements or three elements, depending of the truthness of
cond.
y is a list of 2 elements or three elements, depending on whether bar() is
None.

It also opens the gate for None-coercion operator (discussed recently),
where "x?" is replaced with "*[x if x is None]". If operations on this
expression are mapped into the elements, "x?.foo" becomes "*[x.foo if x is
None]" which is "x.foo" if x is not None, and "*[]" otherwise.

It is similar to except-expression, but without actual explicit exception
handling, and thus much more readable.

Elazar


On Tue, Oct 11, 2016 at 4:08 PM Martti Kühne <mar...@mar77i.ch> wrote:

> Hello list
>
> I love the "new" unpacking generalisations as of pep448. And I found
> myself using them rather regularly, both with lists and dict.
> Today I somehow expected that [*foo for foo in bar] was equivalent to
> itertools.chain(*[foo for foo in bar]), which it turned out to be a
> SyntaxError.
> The dict equivalent of the above might then be something along the
> lines of {**v for v in dict_of_dicts.values()}. In case the values
> (which be all dicts) are records with the same keys, one might go and
> prepend the keys with their former keys using
> {
>     **dict(
>         ("{}_{}".format(k, k_sub), v_sub)
>         for k_sub, v_sub in v.items()
>     ) for k, v in dict_of_dicts.items()
> }
> Was anyone able to follow me through this?
>
> cheers!
> mar77i
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to