Some set operators

Alex Martelli aleax at mail.comcast.net
Sat Oct 15 16:55:46 EDT 2005


<bearophileHUGS at lycos.com> wrote:

> Sometimes I suggest to add things to the language (like adding some set
> methods to dicts), but I've seen that I tend to forget the meaning of
> six set/frozenset operators:
> 
> s & t  s &= t
> s | t  s |= t
> s ^ t  s ^= t
> 
> My suggestion is to remove them, and keep them only as explicit
> non-operator versions (.symmetric_difference(), .update(),
> .intersection_update(), etc). But maybe now it's too much late to
> remove them... Maybe someone gentle can explain me the advantage of
> having/keeping them.

Helen:~ alex$ python2.4 -mtimeit -s's1=s2=set()' 's1&s2'
1000000 loops, best of 3: 0.929 usec per loop

Helen:~ alex$ python2.4 -mtimeit -s's1=s2=set()' 's1.intersection(s2)'
1000000 loops, best of 3: 1.28 usec per loop

Besides avoiding the need for a name look-up, and thus extracting a tiny
speed-up of about 0.35 microseconds or so, I can't think of advantages
for the infix operator form (but then, I can't think of any advantages
for type *int* having the same operators, with bitwise-logic semantics,
rather than placing them only in some library module).

I still vaguely hope that in 3.0, where backwards incompatibilities can
be introduced, Python may shed some rarely used operators such as these
(for all types, of course).  As long as the operators are there for
ints, it makes sense to have them apply to sets as well, of course.


Alex



More information about the Python-list mailing list