<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Sep 10, 2015 at 3:12 PM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a>></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 class=""><div class="h5"><span style="color:rgb(34,34,34)">You could use a generator expression with a function that discards the results:</span><br></div></div>
<br>
def every(iterable):<br>
for _ in iterable:<br>
pass<br>
<br>
every(obj.a_method() for obj in a_sequence)<br></blockquote><div><br></div><div>sure -- though this adds a new function that people reading my code need to grok.</div><div><br></div>Andrew Barnert wrote:<br></div></div><div class="gmail_extra"><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">> [obj.a_method() for obj in a_sequence]</blockquote></div><div class="gmail_extra"><div><span style="font-size:12.8px"><br></span></div><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"><span style="font-size:12.8px">I think this is an anti-pattern. The point of a comprehension is that it's an expression, which gathers up results. You're trying to hide side effects inside an expression, which is a bad thing to do, and lamenting the fact that you get a useless value back, which of course you do because expressions have values, so that should be a sign that you don't actually want an expression here.</span></blockquote><div><br></div><div>Exactly -- I don't want a comprehension, I don't want a expression, I want a concise way to spell : do this thing to all of these things....<br style="font-size:12.8px"><br style="font-size:12.8px"><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"><span style="font-size:12.8px">Also, compare the actual brevity here:</span><span class="im" style="font-size:12.8px"><br></span><span class="im" style="font-size:12.8px"> [obj.a_method() for obj in a_sequence]<br></span><span class="im" style="font-size:12.8px"> for obj in a_sequence: obj.a_method()</span><span class="im" style="font-size:12.8px"><br></span><span style="font-size:12.8px">You've replaced a colon with a pair of brackets, so it's actually less concise.</span></blockquote><div><br></div><div>Fair enough -- removing a newline does make that pretty simple looking!</div><div><br></div><div>I guess I got all comprehension-happy there -- back when it was the shiny new toy, and then I got stuck on it.</div><div><br></div><div>-CHB</div><div><br></div><div><br></div><div> </div></div>-- <br><div class="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>