(PEP-308) Python's Conditional Selection Operators

Steven Taschuk staschuk at telusplanet.net
Sat Feb 15 12:15:18 EST 2003


Quoth Terry Reedy:
> PYTHON'S CONDITIONAL SELECTION OPERATORS: 'AND' AND 'OR'

An interesting discussion.

> [...] For
> efficiency, execution of the second operand can be conditional on the
> first not being dominant.  The possible problem is when execution of b
> has a side-effect that also becomes conditional.

What is "the possible problem" to which you refer?  To me, the
fact that the second operand's side-effects are conditional is one
of the most desirable properties of these operators' definitions.

> Selective 'and' and 'or' have an unusual combination of properties.
> [...] They are non-commutative [...] Both are associative [...]

Also note these properties:
	Homomorphism:
		a and b == c  ==>  bool(a) and bool(b) == bool(c)
		 a or b == c  ==>  bool(a) or bool(b) == bool(c)
	Law of tautology:
		a and a == a or a == a
	Left double distributivity:
		a and (b or c) == (a and b) or (a and c)
		a or (b and c) == (a or b) and (a or c)
	Left absorption:
		a or (a and b) == a
		a and (a or b) == a
And, although we haven't defined 'not' for the new domain (indeed,
'not' can't be selective except for a one-element domain), we do
have
	Weak left cancellativity:
		a and b == a and c  ==>  bool(a) == F or b == c
		 a or b == a or c   ==>  bool(a) == T or b == c
(Note the analogy to rings, in which if ab = ac then a = 0 or b =
c.)  These properties make me feel that selective {and,or} are a
pretty good stab at a non-commutative generalization of boolean
algebras.  Imho they are the most natural way to make them binary
operators on an arbitrary domain (rather than merely relations on
that domain, as the coercing versions are).

(I leave exploring the right-side analogues to the interested
reader.  They're not all straightforward.)

> A non-trival binary logic operator is one whose output depends on both
> inputs.  Of the ten possible, only and and or are selective in that
> the output is always one of the inputs. (To be selective, 0 op 0 and 1
> op 1 must be 0 and 1 respectively.  Of the four possible assignments
> to 0 op 1 and 1 op 0, two produce the trivial selectors a op b == a
> and a op b == b.  The other two are and and or.)  They are thus the
> only two that can be generalized in the way they are in Python.

Nice observation!  This gives another way to show that 'not'
cannot be selective except in trivial cases: if it were, then e.g.
'a and not b' would be selective (since compositions of selective
functions are selective).

-- 
Steven Taschuk           | Receive them ignorant;
staschuk at telusplanet.net | dispatch them confused.
                         |   (Weschler's Teaching Motto)





More information about the Python-list mailing list