<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Jun 28, 2013, at 18:50, Shane Green <<a href="mailto:shane@umbrellacode.com">shane@umbrellacode.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=windows-1252">Yes, but it only works for generator expressions and not comprehensions.</div></blockquote><div><br></div><div>This is the point if options #1 and 2: make StopIteration work in comps either (1) by redefining comprehensions in terms of genexps or (2) by fiat. </div><div><br></div><div>After some research, it turns out that these are equivalent. Replacing any [comprehension] with list(comprehension) is guaranteed by the language (and the CPython implementation) to give you exactly the same value unless (a) something in the comp raises StopIteration, or (b) something in the comp relies on reflective properties (e.g., sys._getframe().f_code.co_flags) that aren't guaranteed anyway.</div><div><br></div><div>So, other than being 4 characters more verbose and 40% slower, there's already an answer for comprehensions.</div><div><br></div><div>And if either of those problems is unacceptable, a patch for #1 or #2 is actually pretty easy. </div><div><br></div><div>I've got two different proof of concepts: one actually implements the comp as passing the genexp to list, the other just wraps everything after the BUILD_LIST and before the RETURN_VALUE in a the equivalent of try: ... except StopIteration: pass. I need to add some error handling to the C code, and for #2 write sufficient tests that verify that it really does work exactly like #1, but I should have working patches to play with in a couple days.</div><br><blockquote type="cite"><div>My opinion of that workaround is that it’s also a step backward in terms of readability. I suspect. </div></blockquote><blockquote type="cite"><div><div><br></div><div>if i < 50 else stop() would probably also work, since it throws an exception. That’s better, IMHO. </div><div><br></div><div><br></div><div><br></div><div><br></div><div><br><div><div><br><div><div>On Jun 28, 2013, at 6:38 PM, Andrew Carter <<a href="mailto:acarter@g.hmc.edu">acarter@g.hmc.edu</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div dir="ltr">Digging through the archives (with a quick google search) <a href="http://mail.python.org/pipermail/python-ideas/2013-January/019051.html">http://mail.python.org/pipermail/python-ideas/2013-January/019051.html</a>, if you really want an expression it seems you can just do<div><br><div>def stop():</div><div> raise StopIteration</div><div>list(i for i in range(100) if i < 50 or stop())</div><div><br></div><div>it seems to me that this would provide syntax that doesn't require lambdas.</div><div><br></div><div><br></div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 28, 2013 at 4:50 PM, Alexander Belopolsky<span class="Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:alexander.belopolsky@gmail.com" target="_blank">alexander.belopolsky@gmail.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 28, 2013 at 7:38 PM, Shane Green<span class="Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:shane@umbrellacode.com" target="_blank">shane@umbrellacode.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;"><div>..</div><div class="im"><div>[x until condition for x in l ...] or </div><div>[x for x in l until condition]</div></div></div></blockquote><div><br></div><div>Just to throw in one more variation:</div><div><br></div><div>[expr for item in iterable break if condition]</div><div><br></div><div>(inversion of "if" and "break"reinforces the idea that we are dealing with an expression rather than a statement - compare with "a if cond else b") </div></div></div></div><br>_______________________________________________<br>Python-ideas mailing list<br><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a></blockquote></div></div></div></blockquote></div><br></div></div></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a></span><br><span><a href="http://mail.python.org/mailman/listinfo/python-ideas">http://mail.python.org/mailman/listinfo/python-ideas</a></span><br></div></blockquote></body></html>