[issue29043] dict view string representations are misleading

Tadhg McDonald-Jensen report at bugs.python.org
Wed Dec 21 23:38:44 EST 2016


New submission from Tadhg McDonald-Jensen:

Currently the string representation for dictionary views are a bit misleading as they look like valid expression but isn't:

    >>> {'a':1, 'b':2}.keys()
    dict_keys(['b', 'a'])
    >>> dict_keys = type({}.keys()) #get a reference to the type
    >>> dict_keys(['b', 'a'])
    Traceback (most recent call last):
      File "<pyshell#24>", line 1, in <module>
        dict_keys(['b', 'a'])
    TypeError: cannot create 'dict_keys' instances


This seems inconsistent with the documentation for 'repr' 
https://docs.python.org/3/reference/datamodel.html#object.__repr__

"If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned."

So I'd think the representation for dictionary views should look something like this instead:

     <dict_keys ['a', 'b']>

to indicate that it cannot be reproduced by a simple expression but still shows it's contents.

----------
messages: 283808
nosy: Tadhg McDonald-Jensen
priority: normal
severity: normal
status: open
title: dict view string representations are misleading
type: behavior
versions: Python 2.7, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29043>
_______________________________________


More information about the Python-bugs-list mailing list