[issue38853] set.repr breaches docstring contract

Steven D'Aprano report at bugs.python.org
Wed Nov 20 15:31:45 EST 2019


Steven D'Aprano <steve+python at pearwood.info> added the comment:

> My confusion stems from the fact that I expected the unpacking of a 
> set to return the same output as that obtained from the unpacking of a 
> list.

Why did you expect that?

Sets aren't lists. Lists are ordered, so they hold their items in a 
specific order. Sets are unordered, so there is no guarantee what order 
you will see when you unpack them.

If you create the list [foo, bar, baz] then the output will always be 
[foo, bar, baz] on every platform. That's a guarantee.

Sets are unordered, as documented, so there are no guarantee about what 
order you will see: it might be {foo, baz, bar} or {bar, baz, foo} or 
{foo, bar, baz} or {baz, foo, bar}, any permutation is equally valid, 
regardless of what order you created the set.

> 1. repr is apparently platform-dependent

Quite likely. Since there's no guarantee what order you will see, 
there's no guarantee that the order won't change from platform to 
platform, or version to version.

> 2. Testing reviewer's assertion: "The specific order you see will 
> depend on the specific values in the set, as well as the order that 
> they were inserted, deleted, and/or re-inserted in some arbitrary 
> way."
> This counter example, where element 0 is moved to the second position, 
> shows that there is not such order dependence:

Your example shows that the output order changes when you change the 
input order, in an unpredicatable, arbitrary way, just like I said. 
That's not a counter-example.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38853>
_______________________________________


More information about the Python-bugs-list mailing list