Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107421

PEP proposal: sequence expansion support for yield statement: yield *

From Ken Seehart <k.seehart@partner.samsung.com>
Newsgroups comp.lang.python
Subject PEP proposal: sequence expansion support for yield statement: yield *
Date 2016-04-20 19:34 +0000
Message-ID <mailman.36.1461181657.12923.python-list@python.org> (permalink)
References <573e61523bfc412ca8674b06d70f2fd7@sraex01sc.sisa.samsung.com>

Show all headers | View raw


Currently the common pattern for yielding the elements in a sequence is as follows:

  for x in sequence: yield x

I propose the following replacement (the result would be identical):

  yield *sequence

The semantics are somewhat different from argument expansion (from which the syntax is borrowed), but intuitive: yield all of the elements of a sequence (as opposed to yield the sequence as a single item). This doesn't appear to have any syntactical collisions, as it is currently a syntax error.

Motivation: More compact notation, and the compiler can produce more efficient bytecode than the former representation (the loop overhead is omitted). This pattern is very common in recursive generators, so a compact notation would be nice.

Also, there is precedent: the proposed notation is implemented in javascript with identical semantics (though in javascript, the conventional spacing is different: yield* sequence ).

Examples:
  yield *(1,2,3)
... instead of :
  yield 1; yield 2; yield 3
... or:
  for x in (1,2,3): yield x


  yield *chain(seq1, seq2)
... instead of :
  for x in chain(seq1, seq2) yield x

~ Ken Seehart

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

PEP proposal: sequence expansion support for yield statement: yield * Ken Seehart <k.seehart@partner.samsung.com> - 2016-04-20 19:34 +0000
  Re: PEP proposal: sequence expansion support for yield statement: yield * Steven D'Aprano <steve@pearwood.info> - 2016-04-21 10:21 +1000
    Re: PEP proposal: sequence expansion support for yield statement: yield * justin walters <walters.justin01@gmail.com> - 2016-04-21 08:19 -0700
    Re: PEP proposal: sequence expansion support for yield statement: yield * Chris Angelico <rosuav@gmail.com> - 2016-04-22 01:26 +1000

csiph-web