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 <rep...@bugs.python.org>
<http://bugs.python.org/issue29043>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to