Disabling sorting mechanism in pprint output

后端 未结 4 1280
鱼传尺愫
鱼传尺愫 2020-12-20 12:01

I have big dictionary which I`m printing for viewing with prettyprint, but how I can keep formatting but kill sorting mechanism in pprint?

4条回答
  •  时光说笑
    2020-12-20 12:04

    I know that I am a bit late here but my preferred way to disable sorting dicts is by using partial:

    from functools import partial
    from pprint import pprint
    
    pprint = partial(pprint, sort_dicts=False)
    

    Personally I like this way as it introduces the smallest diffs.

    It has the benefit of monkey patching in that you only have to make changes at one spot(unlike other options) without having to mess with the internals of pprint.

    I'm using py3.8 but this should work from whenever sort_dicts option was added.

提交回复
热议问题