[Python-Dev] Py2.6 buildouts to the set API

Mike Klaas mike.klaas at gmail.com
Sat May 19 03:50:24 CEST 2007


On 18-May-07, at 6:34 PM, Raymond Hettinger wrote:

> Here some ideas that have been proposed for sets:
>
> * New method (proposed by Shane Holloway):  s1.isdisjoint(s2).   
> Logically equivalent to "not s1.intersection(s2)" but has an early- 
> out if a common member is found.  The speed-up is potentially large  
> given two big sets that may largely overlap or may not intersect at  
> all.  There is also a memory savings since a new set does not have  
> to be formed and then thrown away.

+1.  Disjointness verification is one of my main uses for set(), and  
though I don't think that the early-out condition would trigger often  
in my code, it would increase readability.

> * Additional optional arguments for basic set operations to allow  
> chained operations.  For example, s=s1.union(s2, s3, s4) would be  
> logically equivalent to s=s1.union(s2).union(s3).union(s4) but  
> would run faster because no intermediate sets are created, copied,  
> and discarded.  It would run as if written:  s=s1.copy(); s.update 
> (s2); s.update(s3); s.update(s4).

It's too bad that this couldn't work with the binary operator spelling:

s = s1 | s2 | s3 | s4

> * Make sets listenable for changes (proposed by Jason Wells):
>
>     s = set(mydata)
>     def callback(s):
>          print 'Set %d now has %d items' % (id(s), len(s))
>     s.listeners.append(callback)
>     s.add(existing_element)       # no callback
>     s.add(new_element)            # callback

-1 on the base set type: it seems too complex for a base set type.   
Also, there are various possible semantics that might be desirable,  
such as receiving the added element, or returning False to prevent  
addition.

The proper place is perhaps a subclass of set with a magic method  
(analogous to defaultdict/__missing__).

-Mike


More information about the Python-Dev mailing list