@@ -759,23 +759,27 @@ Warnings
759
759
:mod: `doctest ` is serious about requiring exact matches in expected output. If
760
760
even a single character doesn't match, the test fails. This will probably
761
761
surprise you a few times, as you learn exactly what Python does and doesn't
762
- guarantee about output. For example, when printing a dict, Python doesn't
763
- guarantee that the key-value pairs will be printed in any particular order, so a
764
- test like ::
762
+ guarantee about output. For example, when printing a set, Python doesn't
763
+ guarantee that the element is printed in any particular order, so a test like ::
765
764
766
765
>>> foo()
767
- {"Hermione": "hippogryph" , "Harry": "broomstick "}
766
+ {"Hermione", "Harry"}
768
767
769
768
is vulnerable! One workaround is to do ::
770
769
771
- >>> foo() == {"Hermione": "hippogryph" , "Harry": "broomstick "}
770
+ >>> foo() == {"Hermione", "Harry"}
772
771
True
773
772
774
773
instead. Another is to do ::
775
774
776
- >>> d = sorted(foo().items() )
775
+ >>> d = sorted(foo())
777
776
>>> d
778
- [('Harry', 'broomstick'), ('Hermione', 'hippogryph')]
777
+ ['Harry', 'Hermione']
778
+
779
+ .. note ::
780
+
781
+ Before Python 3.6, when printing a dict, Python did not guarantee that
782
+ the key-value pairs was printed in any particular order.
779
783
780
784
There are others, but you get the idea.
781
785
0 commit comments