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

Forest report at bugs.python.org
Sat Jun 16 08:37:58 EDT 2018


Forest <fgregg at gmail.com> added the comment:

Thank you very much for thorough explanation! It really helped me
understand the issue.

Since this is the intended behavior, would it be good to add some tests for
the behavior? I would have found those tests helpful in working on
https://bugs.python.org/issue27575

If so, I'm happy to prepare a PR for adding some tests for this behavior.

On Sat, Jun 16, 2018 at 2:22 AM Raymond Hettinger <report at bugs.python.org>
wrote:

>
> 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>
> _______________________________________
>

----------

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


More information about the Python-bugs-list mailing list