boolean operations on sets

Michael J. Fromberger Michael.J.Fromberger at Clothing.Dartmouth.EDU
Mon Aug 6 15:03:23 EDT 2007


In article <5hotveF3l5gmkU1 at mid.uni-berlin.de>,
 "Diez B. Roggisch" <deets at nospam.web.de> wrote:

> Flavio wrote:
> 
> > Hi, I have been playing with set operations lately and came across a
> > kind of surprising result given that it is not mentioned in the
> > standard Python tutorial:
> > 
> > with python sets,   intersections  and unions are supposed to be done
> > like this: 
> > [...]
> > 
> > The results are not what you would expect from an AND  or OR
> > operation, from the mathematical point of view! aparently the "and"
> > operation is returning the the second set, and the "or" operation is
> > returning the first.
> > [...]
> 
> It has nothing to do with sets - it stems from the fact that certain values
> in python are considered false, and all others true. And these semantics
> were introduced at a point where there was no explicit True/False, so the
> operators were defined in exact the way you observed.
> 
> Consider this:
> 
> "foo" or "bar" -> "foo"
> 
> So - nothing to do with sets.

In addition to what Diez wrote above, it is worth noting that the 
practise of returning the value of the determining expression turns out 
to be convenient for the programmer in some cases.  Consider the 
following example:

  x = some_function(a, b, c) or another_function(d, e)

This is a rather nice shorthand notation for the following behaviour:

  t = some_function(a, b, c)
  if t:
    x = t
  else:
    x = another_function(d, e)

In other words, the short-circuit behaviour of the logical operators 
gives you a compact notation for evaluating certain types of conditional 
expressions and capturing their values.  If the "or" operator converted 
the result to True or False, you could not use it this way.  

Similarly,

  x = some_function(a, b, c) and another_function(d, e)

... behaves as if you had written:

  x = some_function(a, b, c)
  if x:
    x = another_function(d, e)

Again, as above, if the results were forcibly converted to Boolean 
values, you could not use the shorthand.

Now that Python provides an expression variety of "if", this is perhaps 
not as useful as it once was; however, it still has a role.  Suppose, 
for example, that a call to some_function() is very time-consuming; you 
would not want to write:

  x = some_function(a, b, c) \
      if some_function(a, b, c) else another_function(d, e)

... because then some_function would get evaluated twice.  Python does 
not permit assignment within an expression, so you can't get rid of the 
second call without changing the syntax.

Also, it is a common behaviour in many programming languages for logical 
connectives to both short-circuit and yield their values, so I'd argue 
that most programmers are proabably accustomed to it.  The && and || 
operators of C and its descendants also behave in this manner, as do the 
AND and OR of Lisp or Scheme.  It is possible that beginners may find it 
a little bit confusing at first, but I believe such confusion is minor 
and easily remedied.

Cheers,
-M

-- 
Michael J. Fromberger             | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA



More information about the Python-list mailing list