This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: set() returns random output with Python 3.4.1, in non-interactive mode
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jackson.Cooper, benjamin.peterson, ned.deily
Priority: normal Keywords:

Created on 2014-06-08 06:42 by Jackson.Cooper, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg220021 - (view) Author: Jackson Cooper (Jackson.Cooper) Date: 2014-06-08 06:42
The set() built-in returns random output, only when Python 3 is being used, and in non-interactive mode (executing a file).

Steps to reproduce:

1. Create file with only print(set(['A', 'B'])) inside it.

2. Execute file with Python 3.4.1 numerous times (10+) over 10+ seconds. The output will vary (randomly?) between {'B', 'A'} and {'A', 'B'}.


I can only reproduce this with Python 3.4.1 (have not tried 3.5). It cannot be reproduced in Python 2 (2.7.6) interactive or non-interactive mode, or Python 3.4.1 in interactive mode. Only in Python 3 when executing a file.


Tested on OS X 10.9.3, Python installed via Homebrew.
msg220022 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-06-08 06:52
Yep, set order like dictionary order is arbitrary.
msg220023 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-06-08 06:58
To expand a bit, this is by design, a consequence of the hash randomization feature; see

https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED

As noted there, if necessary, it is possible to disable hash randomization.  But tests with set values or dict keys should not depend on a particular order as even disabling hash randomization would not guarantee the same results on different platforms or builds of Pythons.

$ python3.4 -c "print(set(['A', 'B']))"
{'B', 'A'}
$ python3.4 -c "print(set(['A', 'B']))"
{'A', 'B'}
$ python3.4 -c "print(set(['A', 'B']))"
{'B', 'A'}
$ PYTHONHASHSEED=0 python3.4 -c "print(set(['A', 'B']))"
{'B', 'A'}
$ PYTHONHASHSEED=0 python3.4 -c "print(set(['A', 'B']))"
{'B', 'A'}
$ PYTHONHASHSEED=0 python3.4 -c "print(set(['A', 'B']))"
{'B', 'A'}
msg220024 - (view) Author: Jackson Cooper (Jackson.Cooper) Date: 2014-06-08 07:13
Ah, gotcha. I was assuming the output was consistent across environments, even though ordering of set() is arbitrary.
History
Date User Action Args
2022-04-11 14:58:04adminsetgithub: 65890
2014-06-08 07:13:14Jackson.Coopersetmessages: + msg220024
2014-06-08 06:58:31ned.deilysetnosy: + ned.deily

messages: + msg220023
stage: resolved
2014-06-08 06:52:37benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg220022

resolution: not a bug
2014-06-08 06:42:09Jackson.Coopercreate