Question About Logic In Python

Steve Holden steve at holdenweb.com
Thu Sep 22 05:41:01 EDT 2005


Ron Adam wrote:
> Steven D'Aprano wrote:
> 
> 
>>>So..
>>>
>>>   bool(a and b) * value
>>>
>>>Would return value or zero, which is usually what I want when I do this 
>>>type of expression.
> 
> 
>>That's all very interesting, and valuable advice for somebody who doesn't
>>understand how Python's logical operators work, but the question is, when
>>would you actually want that type of expression?
> 
> 
> It's a filter which returns a value or zero depending on conditions a and b.
> 
> Some examples...
> 
> High pass filter:
> 
>      (value > 0) * value
> 
> Low pass filter:
> 
>      (value < 0) * value
> 
> Band pass filter:
> 
>      (min < value < max) * value
> 
> 
> Changing and and or to return bools only, doesn't prevent us from doing 
> anything we can already do.  It just means changing the context to 
> explicitly return a non bool when you want one as I did here.
> 
> 
>>In practice, how often do you really care that your truth values have the
>>specific values 0 and 1 rather than anything false and anything true? In
>>what circumstances?
> 
> 
> We can separate these into two groups...
> 
>     1. True and false condition testing in which the result of the 
> condition is not used in any further expressions.
> 
> You are correct in this case, it doesn't matter. Any True values would work.
> 
>     2. Expressions that will be used in a calculation or another 
> expression.
> 
By which you appear to mean "expressions in which Boolean values are 
used as numbers".

> This matters because if you aren't careful your results may not be what 
> you expect.
> 
Well yes, but then I wouldn't necessarily expect good results if I tried 
to use a nail file as a tyre-lever, either. If you abuse the intent of 
anything sufficiently you should expect trouble. But then, you seem to 
like trouble ;-)

> But group (2) can also be a part of group (1).  So then again it may 
> matter there too.
> 
> This has more to do with clarity and separating function into forms that 
> have the potential for least surprises.  Or to put it another way, forms 
> that are predictable with no exceptional circumstances.
> 
> In boolean math it is useful to add and subtract.
>  >>> a = b = True
>  >>> a + b
> 2                   # Non boolean result.

Quite.

> 
>  >>> True * True
> 1                   # Why not return True here as well?
> 
Why not return 42? Why not return a picture of a banana?

> This is like adding two integer types and getting a float.
> 
No it isn't. It's like trying to multiply April 2 1994 by June 5 2005. 
The operation isn't defined. So you choose an arbitrary definition and 
say "it would be nice if it worked like this instead of how it actually 
does work".

When in fact it doesn't really "work" at all, except for the most 
tenuous possible value of "work". It's an accident, because Guido 
decided that least code breakage was good when Booleans were introduced.

> 
> There's the possibility of adding two (normally) True values and getting 
> a False result.
> 
>  >>> a = True
>  >>> b = -1
>  >>> a + b      # True_value + True = False_value
> 0
> 
Which is yet another reason why it makes absolutely no sense to apply 
arithmetic operations to Boolean values.
> 
> Should bool type act like bools as expressed here?
> 
>      http://www.ee.surrey.ac.uk/Projects/Labview/boolalgebra/
> 
>      # P1: X = 0 or X = 1
>      # P2: 0 . 0 = 0
>      # P3: 1 + 1 = 1
>      # P4: 0 + 0 = 0
>      # P5: 1 . 1 = 1
>      # P6: 1 . 0 = 0 . 1 = 0
>      # P7: 1 + 0 = 0 + 1 = 1
> 
>      Table 1: Boolean Postulates
> 
> Python's bools work this way if you use 'and' and 'or' and always cast 
> any non bools to bools first.  But it would be easier IMO if bool 
> operations gave bool results so I wouldn't need to do:
> 
>     bool_result = a and bool(b)
> 
> or
> 
>     bool_result = bool(a and b)
> 
You are, of course, ignoring the huge amount of code breakage this 
little change you'd find so convenient would cause.

> On one hand these seem like little things, but little things is 
> sometimes what will bite you the hardest as they are more likely to get 
> by your guard.
> 
Kindly think again about the vast number of times that Python 
programmers have relied on the documented property of "and", which says 
that it returns the left operand (without evaluating the right one) 
unless the left operand is equivalent to False, in which case it returns 
the right operand.

You talk about "least surprises" and "in my opinion" as though your 
opinions are the only ones that anyone would dream of holding. This is 
in itself quite surprising to me.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                          www.pycon.org




More information about the Python-list mailing list