[Python-Dev] Tricky way of of creating a generator via a comprehension expression
Serhiy Storchaka
storchaka at gmail.com
Sat Nov 25 03:16:34 EST 2017
More information about the Python-Dev mailing list
Sat Nov 25 03:16:34 EST 2017
- Previous message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Next message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
24.11.17 02:50, Nick Coghlan пише: > If we went down that path, then a list comprehension like the following: > > results = [(yield future) for future in list_of_futures] > > might be compiled as being equivalent to: > > def __listcomp_generator(iterable): > result = [] > for future in iterable: > result.append((yield future)) > return result > > results = yield from _listcomp_generator(list_of_futures) > > The only difference between the current comprehension code and this idea > is "an explicit yield expression in a comprehension implies the use of > 'yield from' when calling the nested function". Oh, nice! This is a much simpler solution! And it solves all related problems. I like it. This has an overhead in comparison with inlining the code, but the latter can be considered just as an optimization. We can apply it when prove the need in optimizing this construction. For now it is enough if it just works. The fact that two independent mental models lead to the same result is an argument for their correctness. I'm not so sure about "yield" in generators, this will need further thoughts and experiments. "yield" can be used not only in the item expression, but in conditions and inner iterables.
- Previous message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Next message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list