[Python-ideas] Ruby-style Blocks in Python Idea [with examples]
greg.ewing at canterbury.ac.nz
Mon Mar 9 22:33:04 CET 2009
> q = Entry.objects.filter(headline__startswith="What").filter(pub_date__lte=datetime.now())
> using Entry.filter do (entry):
> if entry.headline.startswith('What') and entry.pub_date <= datetime.now():
> return entry
How is this any better than
[entry for entry in Entry.filter
if entry.headline.startswith('What') and entry.pub_date <= datetime.now()]
But note that neither of these is an adequate replacement for
the Django expression, because Django can generate an SQL
query incorporating the filter criteria. Neither a list
comprehension nor the proposed using-statement are capable
of doing that.
The same thing applies to the App Engine example, or any
other relational database wrapper.
By the way, having the 'return' in there doing something
other than return from the function containing the 'using'
statement would be confusing and inconsistent with the
rest of the language.
> # Event-driven Programming
I've been exploring this a bit myself in relation to my
yield-from proposal, and doing this sort of thing using
generators is a much better idea, I think, especially given
something like a yield-from construct.
More information about the Python-ideas