<div dir="ltr">You're right that there's some inconsistency here. But I don't think it's worth fixing given that the fix would introduce another inconsistency (which you pointed out) and would also risk breaking backwards compatibility. I think this ship has sailed.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 30, 2017 at 5:18 PM, Yahya Abou 'Imran via Python-ideas <span dir="ltr"><<a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">=== This proposition is purely aesthetic ===<br>
<br>
At this time, the __repr__ of the mapping views is showing the whole mapping:<br>
<br>
>>> from collections.abc import ValuesView, KeysView, ItemsView<br>
>>> d = {3: 'three', 4: 'four'}<br>
>>> KeysView(d)<br>
KeysView({3: 'three', 4: 'four'})<br>
>>> ValuesView(d)<br>
ValuesView({3: 'three', 4: 'four'})<br>
>>> ItemsView(d)<br>
ItemsView({3: 'three', 4: 'four'})<br>
<br>
Witch is not consistent with dict_keys, dict_values, dict_items:<br>
<br>
>>> d.keys()<br>
dict_keys([3, 4])<br>
>>> d.values()<br>
dict_values(['three', 'four'])<br>
>>> d.items()<br>
dict_items([(3, 'three'), (4, 'four')])<br>
<br>
We could easily change that, since all the views are iterables on what they are designed for, in MappingView:<br>
<br>
def __repr__(self):<br>
viewname = self.__class__.__name__<br>
elements = ', '.join(map(repr, self))<br>
return f'{viewname}([elements])<br>
<br>
And now:<br>
<br>
>>> KeysView(d)<br>
KeysView([3, 4])<br>
>>> ValuesView(d)<br>
ValuesView(['three', 'four'])<br>
>>> ItemsView(d)<br>
ItemsView([(3, 'three'), (4, 'four')])<br>
<br>
It's not breaking any test (it seems that there isn't any for this), but it have a real drawback: it's breaking the convention about instantiation by copy/pasting:<br>
<br>
>>> ValuesView(['three', 'four'])<br>
Traceback (most recent call last):<br>
File "/usr/lib/python3.6/site-<wbr>packages/IPython/core/<wbr>formatters.py", line 684, in __call__<br>
return repr(obj)<br>
File "/usr/lib/python3.6/_<wbr>collections_abc.py", line 706, in __repr__<br>
elements = ', '.join(map(repr, self))<br>
File "/usr/lib/python3.6/_<wbr>collections_abc.py", line 764, in __iter__<br>
yield self._mapping[key]<br>
TypeError: list indices must be integers or slices, not str<br>
<br>
<br>
It's because __init__ in MappingView treat the passed argument -- wich is stored in self._mapping -- as the whole mapping, not just keys, values or items... And all the other methods (__contains__ and __iter__) in the subclasses are using this _mapping attribute to work.<br>
<br>
<br>
So what is to prioritize?<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>