Is python's “set” stable?

后端 未结 7 1102
渐次进展
渐次进展 2020-11-29 07:21

The question arose when answering to another SO question (there).

When I iterate several times over a python set (without changing it between calls), can I assume it

7条回答
  •  北海茫月
    2020-11-29 08:16

    As pointed out, this is strictly an implementation detail.

    But as long as you don’t change the structure between calls, there should be no reason for a read-only operation (= iteration) to change with time: no sane implementation does that. Even randomized (= non-deterministic) data structures that can be used to implement sets (e.g. skip lists) don’t change the reading order when no changes occur.

    So, being rational, you can safely rely on this behaviour.

    (I’m aware that certain GCs may reorder memory in a background thread but even this reordering will not be noticeable on the level of data structures, unless a bug occurs.)

提交回复
热议问题