[IPython-dev] display of dicts?

Chris Barker chris.barker at noaa.gov
Mon Dec 18 23:19:57 EST 2017


On Mon, Dec 18, 2017 at 7:04 PM, Nathan Goldbaum <nathan12343 at gmail.com>
wrote:

> Nope, I believe it's this: https://github.com/ipython/ipython/blob/
> dc9d1c679424589480bb83af64aa5ee21031d311/IPython/lib/pretty.py
>

I see, thanks! Though it does have the same behavior of sorting dicts --
you can see that in:

``_dict_pprinter_factory``

However as of cPYthon 3.6, and officially as of 3.7, dicts will maintain
their insertion order.

So we should probably remove the sorting from the _dict_pprinter_factory.

-CHB




>
>
> On Mon, Dec 18, 2017 at 7:56 PM, Chris Barker <chris.barker at noaa.gov>
> wrote:
>
>> Thanks Nathan,
>>
>> And to confirm, this is the stdlib's pprint module, yes?
>>
>> (which does seem to show the same behavior)
>>
>> -CHB
>>
>>
>>
>> On Mon, Dec 18, 2017 at 5:40 PM, Nathan Goldbaum <nathan12343 at gmail.com>
>> wrote:
>>
>>> IPython does use pretty-printing by default. You can control it with the
>>> %pprint magic, in your IPython configuration, with the
>>> PlainTextFormatter.pprint option, or with the --pprint command line
>>> argument when starting IPython.
>>>
>>> http://ipython.readthedocs.io/en/stable/config/options/termi
>>> nal.html#configtrait-PlainTextFormatter.pprint
>>>
>>>
>>>
>>> On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <chris.barker at noaa.gov>
>>> wrote:
>>>
>>>> As Guido has just declared that dicts will now officially preserve
>>>> order:
>>>>
>>>> https://mail.python.org/pipermail/python-dev/2017-December/151283.html
>>>>
>>>> I was playing around  them in py3.6 ipython, and found some (to me) odd
>>>> behavior:
>>>>
>>>> In [1]: d = {'one':1, 'two':2, 'three':3}
>>>>
>>>> In [2]: d
>>>> Out[2]: {'one': 1, 'three': 3, 'two': 2}
>>>>
>>>> Hmm -- order does not appear to be preserved.
>>>>
>>>> But then:
>>>>
>>>> In [3]: str(d)
>>>> Out[3]: "{'one': 1, 'two': 2, 'three': 3}"
>>>>
>>>> In [4]: repr(d)
>>>> Out[4]: "{'one': 1, 'two': 2, 'three': 3}"
>>>>
>>>> In [5]: d.values()
>>>> Out[5]: dict_values([1, 2, 3])
>>>>
>>>> In [6]: d.keys()
>>>> Out[6]: dict_keys(['one', 'two', 'three'])
>>>>
>>>> In [7]: d.items()
>>>> Out[7]: dict_items([('one', 1), ('two', 2), ('three', 3)])
>>>>
>>>> Order IS preserved.
>>>>
>>>> So presumably iPython is calling sorted() or some such when displaying
>>>> a dict.
>>>>
>>>> Is that that case? Is that documented anywhere?? I can't find it.
>>>>
>>>> And with Python >= 3.6, dict order is preserved, so it would probably
>>>> be better to NOT mess with dict order when displaying them in iPython.
>>>>
>>>> SIDE NOTE:
>>>>
>>>> I had a bit of trouble finding this mailing list -- google still points
>>>> to the old ones on scipy.org. -- maybe we can put a note on the home
>>>> page of those lists that they are been moved??
>>>>
>>>> (I only noticed, 'cause the archives of those stop last March)
>>>>
>>>> -Chris
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> Christopher Barker, Ph.D.
>>>> Oceanographer
>>>>
>>>> Emergency Response Division
>>>> NOAA/NOS/OR&R            (206) 526-6959   voice
>>>> 7600 Sand Point Way NE   (206) 526-6329   fax
>>>> Seattle, WA  98115       (206) 526-6317   main reception
>>>>
>>>> Chris.Barker at noaa.gov
>>>>
>>>> _______________________________________________
>>>> IPython-dev mailing list
>>>> IPython-dev at python.org
>>>> https://mail.python.org/mailman/listinfo/ipython-dev
>>>>
>>>>
>>>
>>> _______________________________________________
>>> IPython-dev mailing list
>>> IPython-dev at python.org
>>> https://mail.python.org/mailman/listinfo/ipython-dev
>>>
>>>
>>
>>
>> --
>>
>> Christopher Barker, Ph.D.
>> Oceanographer
>>
>> Emergency Response Division
>> NOAA/NOS/OR&R            (206) 526-6959   voice
>> 7600 Sand Point Way NE   (206) 526-6329   fax
>> Seattle, WA  98115       (206) 526-6317   main reception
>>
>> Chris.Barker at noaa.gov
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at python.org
>> https://mail.python.org/mailman/listinfo/ipython-dev
>>
>>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at python.org
> https://mail.python.org/mailman/listinfo/ipython-dev
>
>


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20171218/51f2e501/attachment-0001.html>


More information about the IPython-dev mailing list