use set notation for repr of dict_keys?

Terry Reedy tjreedy at udel.edu
Sat Feb 20 12:59:00 EST 2021


On 2/20/2021 2:25 AM, Wolfgang Stöcher wrote:
> Having a dict like
>    d = {'one': 1, 'two': 2}
> the representation of its keys
>    repr(d.keys())
> gives
>    "dict_keys(['one', 'two'])"
> 
> But since the keys are unique, wouldn't a representation using the set 
> notation
> be more intuitive, i.e. what about changing the output of 
> dict_keys.__repr__ to
>      "dict_keys({'one', 'two'})"
> (using curly braces instead of brackets)

 From 3.0 to 3.7?, when dict keys were unordered, that might have made 
sense.  But now that dict keys are insertion ordered, I think the list 
brackets suggesting a significant key order is better.  There is also 
the issue that representation changes can break code and therefore need 
substantial reason.


-- 
Terry Jan Reedy




More information about the Python-list mailing list