[Python-ideas] Cofunctions - an enhancement to yield-from
Scott Dial
scott+python-ideas at scottdial.com
Mon Aug 2 18:21:57 CEST 2010
On 8/1/2010 8:16 PM, Antoine Pitrou wrote:
> On Mon, 02 Aug 2010 12:07:06 +1200
> Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> There are still use cases for
>> 'yield from', when you're dealing with generators that are
>> designed to produce values.
>
> It was the tree-walking example, right? It really looked like it solved
> a non-existing problem.
I disagree with this opinion. I have run into this pattern before and
while I didn't need the whole generator protocol honored, I would've
benefited just from the fact that the proposed "yield from" flattened
the iteration.
A while back there was a proposal about adding an IP address
manipulation library to stdlib that I took issue with. And so I started
to write my own that met the requirements, and when it came time to
write __iter__ for the IPNetwork class, it was quite natural to use
recursive generators:
def __iter__(self):
# can I get a "yield from" please?!
for v in self._iter_more(self._network_address.number,
self._host_mask_bits):
yield v
def _iter_more(self, number_so_far, zerobits):
if zerobits:
bit = zerobits[-1]
zerobits = zerobits[:-1]
# yield all 'x...x0y...y' addresses
for v in self._iter_more(number_so_far, zerobits):
yield v
# yield all 'x...x1y...y' addresses
for v in self._iter_more(number_so_far | (1 << bit), zerobits):
yield v
else:
# construct a proper IPAddress instance for the number
yield self._address_class(number_so_far)
Obviously, it can be flattened by hand, but I doubt it would be as
obvious to read later. I have run into this pattern in other cases where
I was writing my own (specialized) containers, and would expect others
to have as well, unless they were uncomfortable with generators and/or
wrote around the problem.
--
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu
--
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu
More information about the Python-ideas
mailing list