[issue33874] dictviews set operations do not follow pattern of set or frozenset

Raymond Hettinger report at bugs.python.org
Sat Jun 16 03:22:23 EDT 2018


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

It's true the concrete set API differs in some ways from the Set abstract base class followed by dictviews.   The concrete set-to-set operators are restricted to only work with other sets, leaving the named set methods (union, intersection, difference, etc) to accept any iterable.  In contrast, the Set abstract base class only has operators and those are specifically allowed to accept any iterable.

It may not seem harmonious, but those were intentional and long-standing design decisions.  The restriction on concrete set operators to only work with other sets can be traced back to bad experiences with the += operator for lists accepting any iterable (allowing mistakes like s+='abc' when s.append('abc') was intended).  

Different choices were made in the design of the abstract Set API.  In order to be useful, that API can't make as strong of a restriction, so it allows any iterable to be used as inputs to the operators.  Also note that the abstract Set API doesn't have the named set methods (union, intersection, difference, etc), so the burden of falls on the operators to support iterables.   IIRC, the reason that the named set methods were omitted was to make it easier to implement conforming classes that could interoperate with one another.  For more details on the design of the collections ABCs, see Guido's PEP on the subject (that's where he explains was problem the abstract classes where intended to solve and some of design considerations).

One can argue with those design decisions, but that ship sailed a long time ago and it would no longer be possible to change either set or Set without breaking existing code.  The existing behaviors are intentional, venerable, tested, and guaranteed.

----------
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33874>
_______________________________________


More information about the Python-bugs-list mailing list