[Python-ideas] Fwd: quantifications, and tuple patterns
Jim Jewett
jimjjewett at gmail.com
Mon Jan 16 21:06:29 CET 2012
On Sat, Jan 14, 2012 at 9:01 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Sat, Jan 14, 2012 at 1:38 PM, Paul Moore <p.f.moore at gmail.com> wrote:
>>> Capturing a (quantification) "witness", which can be done using
>>> assignment-as-expression
> If I recall correctly, there have been occasional discussions about
> changing any() and all() to return the item found rather than a flag.
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.
-jJ
More information about the Python-ideas
mailing list