[Python-Dev] Tricky way of of creating a generator via a comprehension expression

Antoine Pitrou solipsis at pitrou.net
Sat Nov 25 02:24:31 EST 2017


On Sat, 25 Nov 2017 17:10:12 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 25 November 2017 at 16:18, Nathaniel Smith <njs at pobox.com> wrote:
> > On Fri, Nov 24, 2017 at 9:39 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:  
> >> On 25 November 2017 at 15:27, Nathaniel Smith <njs at pobox.com> wrote:  
> >>> On Fri, Nov 24, 2017 at 9:04 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:  
> >>>>     def example():
> >>>>         comp1 = yield from [(yield x) for x in ('1st', '2nd')]
> >>>>         comp2 = yield from [(yield x) for x in ('3rd', '4th')]
> >>>>         return comp1, comp2  
> >>>
> >>> Isn't this a really confusing way of writing
> >>>
> >>> def example():
> >>>     return [(yield '1st'), (yield '2nd')], [(yield '3rd'), (yield '4th')]  
> >>
> >> A real use case  
> >
> > Do you have a real use case? This seems incredibly niche...  
> 
> That's not how backwards compatibility works - we were suggesting
> getting rid of this syntax, because there was no current way to make
> it do anything sensible.
> 
> It turns out there is a way to make it behave reasonably - you just
> need to stick "yield from" in front of it, and it goes back to being
> equivalent to the corresponding for loop (the same as the synchronous
> version).

I don't know if it behaves reasonably, but it's entirely unreadable to
me.  I think there's a case for discouraging unreadable constructs.

Regards

Antoine.




More information about the Python-Dev mailing list