List Comprehensions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Dec 22 10:18:06 EST 2014


Chris Angelico wrote:

> On Mon, Dec 22, 2014 at 8:21 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> If the called function has side-effects, a list comp is not a good
>> solution.
> 
> Hmm. I'm not so sure about that. Side effects are fine in a list comp,
> as long as you're making use of the return values. For instance, if
> you have a function that inserts a record to a database and returns
> the new ID, you could reasonably use a list comp to save a bunch of
> records and get a list of IDs for subsequent use.

I hear what you are saying, but a list comprehension is a functional idiom.
To mix functional and procedural idioms in the one expression is rather
icky. Better to use one or the other but not both simultaneously.

I'll accept that this is a weak recommendation though.


-- 
Steven




More information about the Python-list mailing list