[Python-ideas] How assignment should work with generators?
Steven D'Aprano
steve at pearwood.info
Mon Nov 27 09:46:24 EST 2017
On Tue, Nov 28, 2017 at 01:01:01AM +1100, Chris Angelico wrote:
> And the status quo is noisy and has duplicated
> information (you have to match the ", 2" to the number of assignment
> targets).
Er, not really. How about:
a, b = islice(iterable, 3, 10, 4)
A somewhat unusual case, to be sure, but still legal.
> I would support any syntax that (a) is currently illegal,
> (b) reads reasonably well, and (c) can't be TOO easily confused with
> something else.
Honestly, I don't see this as such a big deal that Python needs to
support it at all: maybe +0.25 on the idea of non-consuming iterable
unpacking. islice does the job. If we have it at all, it is yet another
special syntax to learn, and I'm not convinced that the benefit outways
the cost of learning yet more Perlish magic syntax for a marginal
use-case.
Especially if the syntax looks like grit on Tim's monitor.
On the other hand, if we did get this, I would prefer magic syntax over
a backwards incompatible change.
On balance, given that * is already used for at least 11 different
things already[1], I'd actually prefer the grit on the monitor. Perhaps
x, y, ... = iterable to indicate non-consuming iterable unpacking.
Or maybe
x, y, / = iterable
since in some sense this is the opposite of unpacking -- the whole point
is to NOT unpack the remaining items. And the slash kind of looks like
cutting off the process from continuing: unpack two items, then stop.
... maybe +0.5 with the slash syntax *wink*
[1] Multiplication, exponentiation, sequence replication, *varargs,
**keyword varargs, regexes, globs, import wild cards, iterable
unpacking in function calls, extended iterable unpacking in assignments,
dict unpacking -- have I missed any?
--
Steve
More information about the Python-ideas
mailing list