Well, first, these two sets aren't identical in ordering. At a glance, they flip the ordering of 'n'
and 'f'
.
Beyond that, while set ordering isn't guaranteed in standard set
s in Python as a language, individual implementations may implement some ordering type. Whether that's a reliable contract will ultimately be a function of how much you trust that specific implementation and their promise to offer that as a stable behaviour.
Based on CPython's set
, (of which the meat and potatoes of the insertion implementation lives here), it looks like there's no particular care taken to preserve any specific ordering, nor is there any specific care taken to randomize the order beyond using object hashes, which are stable for any object's lifetime and tend to be stable globally for certain special values (like integers below 256, and individual bytes from the ASCII range in string data).
The same can be said for the implementation of set
's __repr__
, (here), which makes no special effort to randomize or stabilize the order in which items are presented.
Emphatically, though, these are implementation details of CPython. You shouldn't rely on this unless you positively have to, and even then, I'd step back and reevaluate why you're in that position.