On Thu, Sep 10, 2015 at 3:12 PM, MRAB <python@mrabarnett.plus.com> wrote:
You could use a generator expression with a function that discards the results:

def every(iterable):
    for _ in iterable:
        pass

every(obj.a_method() for obj in a_sequence)

sure -- though this adds a new function that people reading my code need to grok.

Andrew Barnert wrote:
> [obj.a_method() for obj in a_sequence]

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.

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....

Also, compare the actual brevity here:
    [obj.a_method() for obj in a_sequence]
    for obj in a_sequence: obj.a_method()
You've replaced a colon with a pair of brackets, so it's actually less concise.

Fair enough -- removing a newline does make that pretty simple looking!

I guess I got all comprehension-happy there -- back when it was the shiny new toy, and then I got stuck on it.

-CHB


 
--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov