Interesting list() un-optimization

Roy Smith roy at panix.com
Fri Mar 8 19:45:02 CET 2013


In article <513a26fa$0$30001$c3e8da3$5496439d at news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:

> On Wed, 06 Mar 2013 22:20:11 -0500, Roy Smith wrote:
> 
> > I stumbled upon an interesting bit of trivia concerning lists and list
> > comprehensions today.
> > 
> > We use mongoengine as a database model layer.  A mongoengine query
> > returns an iterable object called a QuerySet.  The "obvious" way to
> > create a list of the query results would be:
> > 
> >     my_objects = list(my_query_set)
> > 
> > and, indeed, that works.  But, then I found this code:
> > 
> >    my_objects = [obj for obj in my_query_set]
> > 
> > which seemed a bit silly.  I called over the guy who wrote it and asked
> > him why he didn't just write it using list().  I was astounded when it
> > turned out there's a good reason!
> 
> And why was that not documented in the code?
> 
> I hope you took this fellow out and gave him a damned good thrashing!

Nah.  I'm a pacifist.  Besides, he's my boss :-)



More information about the Python-list mailing list