[Python-ideas] Fwd: quantifications, and tuple patterns

Tom Rothamel tom at rothamel.us
Tue Jan 17 02:57:07 CET 2012

(Sorry about the formatting - I'm a former student of Annie's, who just
joined the list.)

Jim Jewett wrote:

> Even in this thread, there have been multiple posts that would shown
> bugs when the witness itself had a boolean evaluation of False.
> What if any and all started to return a Witness object that evaluated
> to True/False, but made the value available?  I could see an object
> like this being useful in other code as well.
>     class Witness(object):
>         def __bool__(self):  return self.truth
>         def __init__(self, truth, value):
>             self.truth=truth
>             self.value=value
> Of course, it might be nicer to inherit from type(True), and it still
> doesn't quite solve the laziness problem with for, or the
> assign-in-the-right-place problem with while.

I'm wondering if would make sense to add an "as" clause to the while and if
statements, similar to the as clause in the with statement. It would
essentially retrieve the value field from the witness object described

This would let one write the main loop in a topological sort (a classic
workset algorithm) as:

    while any(v for v in vertices if v.incoming_count == 0) as v1:
        for v2 in v1.outgoing:
            v2.incoming_count -= 1

Another use of this could be for database queries that return 0 or 1

    if db.query(page_name=page_name) as page:

This is a common pattern that often involves either exception handling or a
sentinel value like None.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120116/e3994cc6/attachment.html>

More information about the Python-ideas mailing list