12

I was just checking out some docs on collections.abcs for a project of mine, where I need to do some type-related work. Those are the official docs about the ValuesView type, in both Python 2 and 3:

and this is the source (Python 2, but same happens in Python 3)

I was very puzzled about the ValuesView interface, because from a logical standpoint it should inherit from Iterable, IMHO (it's even got the __iter__ Mixin method); on the contrary the docs say that it just inherits from MappingView, which inherits from Sized, which doesn't inherit from Iterable.

So I fired up my 2.7 interpreter:

>>> from collections import Iterable
>>> d = {1:2, 3:4}
>>> isinstance(d.viewvalues(), Iterable) 
True
>>>

It looks Iterable, after all, because of Iterable's own subclasshook.

But I don't understand why ValuesView isn't explicitly Iterable. Other ABCs, like Sequence or Set, are explicitly Iterable. Is there some arcane reason behind that, or it's just a documentation+implementation shortcoming for a little-used feature?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Alan Franzoni
  • 3,041
  • 1
  • 23
  • 35

1 Answers1

5

In Python 3.7.2, it inherits from both MappingView and Collection which in turn inherits from (among others) Iterable. Someone listened to you.

Neuron
  • 5,141
  • 5
  • 38
  • 59
LemonPy
  • 500
  • 4
  • 12
  • 2
    https://bugs.python.org/issue32467 "I'm going forward with this because it does offer a sense of neatness (in comparison to KeysView and ItemsView) and it might avoid a pedantic StackOverflow question somewhere down the line." -> actually, the question had already been asked :-) – Alan Franzoni Jan 23 '19 at 20:24