Python uses a RNG when generating hashs for various tasks such as associated with sets and dicts. So for ex, this means that when we create a set and print the items of the set the order might be different in each run of the python script.

So take the below python code and run it a couple of times from the terminal and you might see different order of set elements being printed.

words = {"banana", "date", "apple", "cherry"}
print(list(words))

Note: This is not an issue when we run the code from inside an interactive session (like IPython) since it might already have locked in a hash seed based on how python was launched

Hmm. This is not good (in many cases) when we want deterministic behaviour. So how can we fix this. We have to set an environment variable PYTHONHASHSEED to a particular seed to make things consistent and deterministic.

Now a little caveat. Typically we would set the environment variable in the python script as below,

import os

os.environ["PYTHONHASHSEED"] = "42" # --> set the environment variable within the script

words = {"banana", "date", "apple", "cherry"}
print(list(words))

But this doesnt actually make the python hashing functions deterministic because PYTHONHASHSEED needs to be set before python starts up. Setting it inside the script it too late - the interpreter has already initialized its internal hashing behaviour.

So on windows poweshell we can set PYTHONHASHSEED as below before running the script.

$env:PYTHONHASHSEED = "42"

And now we will see the same order of the set elements printed.