Steven D'Aprano wrote: > On Fri, 06 Jul 2018 09:31:50 +1000, Cameron Simpson wrote: > >> On 05Jul2018 17:57, Steven D'Aprano >> <steve+comp.lang.pyt...@pearwood.info> wrote: >>>I have a function which returns a dict, and I want to use doctest to >>>ensure the documentation is correct. So I write a bunch of doctests: >>> >>>def func(arg): >>> """blah blah blah >>> >>> >>> func(1) >>> {'a': 1, 'b': 2, 'c': 3} >>> """ >>> >>>which is correct, *except* that dict keys have arbitrary order in the >>>versions of Python I'm using. >>> >>>I have three ways of dealing with this. Which do you prefer? >> >> Option 4: >> >> >>> func(1) == {'a': 1, 'b': 2, 'c': 3} >> True > > Alas, in reality func takes anything up to six arguments, at least one of > which will be a moderately long sequence requiring two lines: > > >>> func([('a', 1), ('bb', 2), ('ccc', 4), ('dddd', 8)], > ... ('eee', 16), ('ff', 32), ('g', 64)], ... > > and the output is similarly long. So making it a one-liner isn't > generally practical.
In that case: goodbye doctest, hello unittest ;) With complex or just long setup, underdefined output, and exceptions I usually ditch doctest, convenient as it may be. PS: the tinkerer in me wants to provide import sys class name(str): def __repr__(self): return str(self) def dh(obj): if type(obj) is dict: try: obj = name( "{" + ", ".join("%r: %r" % kv for kv in sorted(obj.items())) + "}" ) except TypeError: pass # might sort by (repr(key), repr(value)) _dh(obj) _dh = sys.__displayhook__ sys.__displayhook__ = dh def f(a): """ >>> f(dict(a=1, b=2)) {'a': 1, 'b': 2} """ return a -- https://mail.python.org/mailman/listinfo/python-list