Non-deterministic output
Peter Otten
__peter__ at web.de
Mon Mar 28 08:08:15 EDT 2011
Esben Nielsen wrote:
> Hi,
>
> We are making a prototype program in Python. I discovered the output was
> non-deterministic, i.e. I rerun the program on the same input files and
> get different output files. We do not use any random calls, nor
> threading.
>
> One of us thought it could be set and dictionaries not always yielding
> the same results. I, however, would think that given the exact same
> operations, a set/dictionary would always yield the same results. Am I
> correct? Or could different runs of the same program yield different
> results due to, say, different memory locations?
If you insert items into a dict/set in the exact same order and the hash
values of the keys are the same across different runs of the program the
order of the dict/set items should be the same.
One way you could inadvertently bring the memory location into play is the
default __hash__() method:
$ cat print_set.py
names = "alpha", "beta", "gamma", "delta", "epsilon"
class A(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
print set(A(name) for name in names)
$ python print_set.py
set([alpha, beta, delta, epsilon, gamma])
$ python print_set.py
set([alpha, beta, delta, gamma, epsilon])
More information about the Python-list
mailing list