Does anyone really know how typing order is determined in Python?

There seems to be some consistency in that calling set() on a string always seems to resolve the same (non-abelian) order, and both

 set([1,2,3]) & set([1,2,3,4]) 

and his mixed cousin

 set([2,3,1]) & set([4,3,1,2]) 

will result in an ordered set([1,2,3]) .

On the other hand, something like a little brighter, for example

 from random import randint set([randint(0,9) for x in range(3)]) 

sometimes it gives something like set([9, 6, 7]) ...

... what's going on here?

+4
source share
1 answer

You should treat sets as unordered collections

They are stored in a hash table.

In addition, as items are added, the hash will be moved to a large table, so the order can change dramatically.

There is no guarantee that the order will be the same for different versions / implementations of Python.

+5
source

All Articles