Set operations on object attributes question

Raymond Hettinger python at rcn.com
Tue Oct 23 17:17:14 EDT 2007


[Duane]
> Since what I _really_ wanted from this was the intersection of the
> objects (based on attribute 2), I was looking for a set-based
> solution,
> kinda like a decorate - <set operation> - undecorate pattern. Perhaps
> the problem does not fall into that category.

Decorating and undecorating do not apply to your example.  If all the
input were decorated with new equality and hash methods, then the
intersection step would return just a single element, not the two that
you specified.

Compare this with ints and floats which already have cross-type
equality and hash functions.  Running, set([1]).intersection([1.0]),
would you expect set([1.0]) or set([1, 1.0])?  Your example specified
the latter behavior which has the unexpected result where the
set intersection returns more elements than exist in either input.


Raymond




More information about the Python-list mailing list