[Python-3000] Adding sorting to generator comprehension

Ian Bicking ianb at colorstudy.com
Mon Apr 24 18:22:40 CEST 2006


In another thread ("Brainstorming: Python Metaprogramming") it occurred 
to me that many LINQish things are reasonable enough given the AST and 
generator expressinos.  But sorting is hard to do.  Sorting also remains 
a place where lambdas are still frequently needed, like:

   sorted(list_of_people, key=lambda p: (p.lname, l.fname))

We got rid of the lambda-encouraging map and filter, maybe one more?

   (p for p in list_of_people orderby (p.lname, p.fname))

I have no particular opinion on the keyword, though I assume a keyword 
is required; it may be difficult to find a keyword that people are not 
frequently using (but then this is py3k, so maybe not as big a deal). 
By including this in the generator expression AST introspection makes it 
possible to translate that to a SQL ORDER BY clause; I'm sure other 
out-of-Python query processors (like one of the numeric packages) could 
use this similarly.

But, putting the AST stuff aside, I also think it is just nice syntax 
for a fairly common case, and a nice compliment for comprehensions.

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list