Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer in general is <strong>no.</strong> The python source that @Christophe and @Marcin (un)helpfully point to shows that elements are popped in the order they appear in the hash table. So, pop order (and presumably iteration order) <em>is</em> deterministic, but only for <em>fixed</em> hash values. That's the case for numbers but <em>not</em> for strings, according to the <strong>Note</strong> in the documentation of <a href="https://docs.python.org/3.5/reference/datamodel.html#object.__hash__"><code>__hash__</code></a>, which incidentally also touches on your question directly:</p> <blockquote> <p>Note by default the <strong>hash</strong>() values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python. </p> <p>[ ... ]</p> <p>Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).</p> </blockquote> <p><strong>Edit:</strong> As @Marcin points out, the link I quoted does not apply to Python 2. Hash randomization <a href="https://docs.python.org/3/whatsnew/3.3.html#summary-release-highlights">became the default with Python 3.3.</a> Python 2.7 does not have intentionally non-deterministic string hashing by default.</p> <p>In general, this is a problem for any object whose hash is not a repeatable function of its value (e.g., if the hash is based on memory address). But conversely, if you define your own <code>__hash__</code> method for the objects in your sets, you can expect that they will be returned in a reproducible order. (Provided the set's history and the platform are kept fixed).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload