[Python-ideas] Fwd: unpacking generalisations for list comprehension

Neil Girdhar mistersheik at gmail.com
Sat Oct 15 13:38:15 EDT 2016


On Sat, Oct 15, 2016 at 5:01 AM Steven D'Aprano <steve at pearwood.info> wrote:

> On Thu, Oct 13, 2016 at 01:30:45PM -0700, Neil Girdhar wrote:
>
> > From a CPython implementation standpoint, we specifically blocked this
> code
> > path, and it is only a matter of unblocking it if we want to support
> this.
>
> I find that difficult to believe. The suggested change seems like it
> should be much bigger than just removing a block. Can you point us to
> the relevant code?
>
>
The Grammar specifies:

dictorsetmaker: ( ((test ':' test | '**' expr)
                   (comp_for | (',' (test ':' test | '**' expr))* [','])) |
                  ((test | star_expr)
                   (comp_for | (',' (test | star_expr))* [','])) )

In ast.c, you can find:

                if (is_dict) {
                    ast_error(c, n, "dict unpacking cannot be used in "
                            "dict comprehension");
                    return NULL;
                }
                res = ast_for_dictcomp(c, ch);

and ast_for_dictcomp supports dict unpacking.

Similarly:

    if (elt->kind == Starred_kind) {
        ast_error(c, ch, "iterable unpacking cannot be used in
comprehension");
        return NULL;
    }

    comps = ast_for_comprehension(c, CHILD(n, 1));

and ast_for_comprehensions supports iterable unpacking.

In any case, it isn't really the difficulty of implementation that is
> being questioned. Many things are easy to implement, but we still
> don't do them.


If it doesn't matter, why bring it up?


> The real questions here are:
>
> (1) Should we overload list comprehensions as sugar for a flatten()
> function?
>
> (2) If so, should we spell that [*t for t in iterable]?
>
>
> Actually the answer to (1) should be "we already do". We just spell it:
>
>     [x for t in iterable for x in t]
>
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/ROYNN7a5VAc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161015/7ec67de6/attachment-0001.html>


More information about the Python-ideas mailing list