On Fri, Jul 27, 2018 at 05:30:35AM +0000, Robert Vanden Eynde wrote:
> Currently, what's the best way to implement a function
> f(OrderedDict([(1,2),(3,4)])) == '{1: 2, 3: 4}', works for all
> possible types, and also availaible for pprint with nice indent?
I don't understand the question, and I especially don't understand the
"all possible types" part. Do you actually mean *all* possible types,
like int, float, str, MyClassThatDoesSomethingWeird?
But guessing (possibly incorrectly) what you want:
py> from collections import OrderedDict
py> od = OrderedDict([(1,2),(3,4)])
py> od == {1:2, 3: 4}
True
If efficiency is no concern, then the simplest way to get something that
has a dict repr from an OrderedDict is to use a dict:
py> dict(od)
{1: 2, 3: 4}
This also works as OrderedDict is a subclass of dict, and should avoid
the cost of building an entire dict:
py> dict.__repr__(od)
'{1: 2, 3: 4}'
> If I
> could set a parameter in ipython or python repl that would print that,
> that would already be very useful.
See sys.displayhook.
--
Steve
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/