Linq to Python
duncan.booth at invalid.invalid
Mon Oct 27 22:40:28 CET 2008
"Tim Rowe" <digitig at gmail.com> wrote:
>> I haven't yet had occasion to use LINQ in anger yet, so I have no
>> idea whether its an idea to love or to hate. I do think it is good
>> that C# has effectively sprouted list comprehensions (not to mention
>> anonymous types and type inferencing) and I expect there may be some
>> aspects worth looking at for Python but I think they are more likely
>> to lead to itertools functions than extensions to syntax.
> Yes, looking at what LINQ adds to C# (according to
> - Implicitly typed variables: Python already has.
> - Object and collection initialisers: Not sure whether Python can do
> this directly, but it can certainly emulate it with a dictionary.
> - Anonymous types: Not sure whether Python can do this directly, but
> it can certainly emulate it with a dictionary.
> - Extension methods: Python already has.
> - Lambda expressions: Python already has.
> - Auto-Implemented properties: No, but that's just syntactic sugar to
> make declarations more compact.
> So all of the language elements that are needed for LINQ are already
> in Python; a library should do the trick.
Not quite. The C# implementation of lambda expressions has a neat trick
that is central to LINQ. A lambda expression compiles either to executable
code, or to an expression tree. If you are filtering some C# sequence
object then (as with Python lambdas) the LINQ code simply calls the
compiled lambda expression like any other delegate, but if you are
filtering a SQL lookup the LINQ code gets an expression syntax tree and
further compiles it (at runtime) into SQL code to perform the filtering at
the database level.
Python can't do the compile time matching to vary what it produces
according to how the lambda is used, but it would be perfectly possible for
function objects which are compiled from a lambda expression to grow an
extra attribute that would hold an AST for the expression. That way you
could write code that took a filter function and, if it had the AST
attribute available, compiled it to SQL or xpath or whatever else you
More information about the Python-list