Re: boolean operations on sets



On Mon, 06 Aug 2007 14:13:51 +0000, 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:
In [7]:set('casa') & set('porca')
Out[7]:set(['a', 'c'])

In [8]:set('casa') | set('porca')
Out[8]:set(['a', 'c', 'o', 'p', 's', 'r'])

and they work correctly. Now what is confusing is that if you do:

In [5]:set('casa') and set('porca')
Out[5]:set(['a', 'p', 'c', 'r', 'o'])

In [6]:set('casa') or set('porca')
Out[6]:set(['a', 'c', 's'])

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.

That might be, because `and` and `or` are not mathematical in Python (at
least not as you think). All the operator bits, e.g. `|` and `&`, are
overloadable. You can just give them any meaning you want.

The `and` and `or` operator, though, are implemented in Python and there
is no way you can make them behave different from how they do it by
default. It has been discussed to remove this behaviour or make them
overloadable as well but this hasn't made it far, as far as I remember.

If python developers wanted these operations to reflect the traditional
(Python) truth value for data structures: False for empty data
structures and True otherwise, why not return simply True or False?

Because in the most cases, returning True of False simply has no
advantage. But returning the actual operands has been of fairly large
use, e.g. for replacing the if expression ("ternary operator") ``THEN if
COND else DEFAULT`` with ``COND and THEN or DEFAULT`` (which has some bad
corner cases, though).

So My question is: Why has this been implemented in this way? I can see
this confusing many newbies...

Hmm, you could be right there. But they shouldn't be biased by default
boolean behaviour, then, anyways.
.



Relevant Pages

  • Re: Lists implemented as integer-hashed Dictionaries?
    ... Yeah, as Chris said, Python lists are not dictionaries at all. ... I believe Python sets *are* for all intents and purposes ... very-well-tuned dictionary implementation and cut out the stuff not ...
    (comp.lang.python)
  • Re: boolean operations on sets
    ... with python sets, intersections and unions are supposed to be done ... Now what is confusing is that if you do: ... traditional truth value for data structures: False for empty ...
    (comp.lang.python)
  • Re: Questions on Using Python to Teach Data Structures and Algorithms
    ... Using Python to teach data structures and algorithms to ... I think Python is only partially fit for your purpose. ... One of the basic data structures is the chained list, ...
    (comp.lang.python)
  • Re: Ordering python sets
    ... As Python becomes accepted for more and more "serious" projects some ... SortedSet, SortedDict: can be based on red-black trees. ... I use all those data structures in Python programs, plus some more, ... A problem with the Chain data structure is how to represent iterators ...
    (comp.lang.python)
  • Re: variable question
    ... tutoring some kids about basics of programming using python. ... order data structures. ... The important thing is really that Python's approach of references is ...
    (comp.lang.python)