Issue 1618676: Promote list comprehensions to Language Integrated Query? (original) (raw)

Hi,

A feature idea (shall put to mailing list discussion sometime), is to extend list comprehensions to many uses of SQL or XQuery unnecessary.

The benefit of language integration over passing SQL/XQuery strings to external libraries would be making code agnostic to the source of the data: in-memory tables and trees could be queried just like a relational DB or XML. An "order by" could be more efficient than sorted(f(x) for x in y).

Of course list comprehensions do much of the C#-style LINQ already, but they can't (easily) do joins or "select x from a, y from b" type queries, or return XML a la XQuery.

On the library side, DBAPI helps make code independent of any particular database, and sqlite in 2.5 means in-memory tables (lists of tuples) aren't really necessary.

If worthwhile, implementation would probably be best suited to PyPy.

C# Example from http://en.wikipedia.org/wiki/Language_Integrated_Query

var q = from o in db.Orders, c in db.Customers where o.Quality == "200" && (o.CustomerID == c.CustomerID) select new { o.DueDate, c.CompanyName, c.ItemID, c.ItemName };

There have been various discussions about metasyntax proposals and Python, see the Python-3000 mailing list for references to LINQ. The general answer has been "no". Note that you can do much of what you want by overloading operations on standard Python objects like or, and, etc.