"for" with "else"?

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Wed Oct 1 23:59:46 EDT 2003


On Wed, 01 Oct 2003 20:10:03 -0700, David Eppstein
<eppstein at ics.uci.edu> wrote:

>Haven't you inverted the logic here?
>You want a universal quantifier, not an existential one.

Yes, absolutely correct.

I did some hacking around with different ways of representing this.
The condition got inverted a few times. The final version was derived
from something like this...

   if not mylib.exists (lambda c : c not in string.printable, data) :
     finfo.Type = 'TEXT'

And I forgot to switch the quantifier when I inverted the logic for
some bizarre reason. I guess that's what happens when you spend too
much time obsessing over a piece of code - the longer you spend
fussing over it the greater the odds of making a stupid mistake, and
the less likely you are to spot it :-(

So yes, the debugged version is...

   if mylib.always (lambda c : c in string.printable, data) :
     finfo.Type = 'TEXT'

with...

  def always (pred, seq) :
    for i in seq :
      if not pred(i) : return False
    return True


While these 'every' and 'always' functions are trivial, they are also
pretty useful and obviously based on the common discrete maths
notation so not unique to me.

Could some simple functions like this have a place in the library
somewhere?


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list