Raymond Hettinger <raymond.hettin...@gmail.com> added the comment:

> After I generate an UML diagram from collections.abc, I found 
> very strange that MappingView inherit from Sized instead
> of Collection (new in python 3.6).

That isn't strange at all.  MappingView predates Collection, so of course it 
didn't inherit from collection.

> Yes, MappingView only define __len__ and not __iter__ 
> and __contains__, but all of its subclasses define them 
> (KeysView, ValuesView and ItemViews).

It is irrelevant what extra behaviors subclasses may or may not add.  The 
MappingView ABC is a public API and users are free to use it in other ways (as 
long as they stick with the publicly document API).  For example, the following 
is allowed (even it seems odd to you):

    >>> from collections.abc import MappingView
    >>> mv = MappingView(['a', 'b'])
    >>> len(mv)                        # guaranteed
    2
    >>> repr(mv)                       # guaranteed
    "MappingView(['a', 'b'])"
    >>> 'a' in mv                      # not guaranteed 
    Traceback (most recent call last):
      File "<pyshell#9>", line 1, in <module>
        'a' in mv
    TypeError: argument of type 'MappingView' is not iterable
    >>> list(mv)                       # not guaranteed
    Traceback (most recent call last):
      File "<pyshell#10>", line 1, in <module>
        list(mv)
    TypeError: 'MappingView' object is not iterable

----------
assignee:  -> rhettinger
nosy: +rhettinger

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32449>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to