change of random state when pyc created??

Peter Otten __peter__ at web.de
Wed May 9 03:29:34 EDT 2007


Alan Isaac wrote:

> This may seem very strange, but it is true.
> If I delete a .pyc file, my program executes with a different state!

> Can someone explain this to me?

There is nothing wrong with the random module -- you get the same numbers on
every run. When there is no pyc-file Python uses some RAM to create it and
therefore your GridPlayer instances are located in different memory
locations and get different hash values. This in turn affects the order in
which they occur when you iterate over the GridPlayer.players_played set.

Here is a minimal example:

import test # sic

class T:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "T(name=%r)" % self.name

if __name__ == "__main__":
    print set(T(i) for i in range(4))

$ python2.5 test.py
set([T(name=2), T(name=1), T(name=0), T(name=3)])
$ python2.5 test.py
set([T(name=3), T(name=1), T(name=0), T(name=2)])
$ python2.5 test.py
set([T(name=3), T(name=1), T(name=0), T(name=2)])
$ rm test.pyc
$ python2.5 test.py
set([T(name=2), T(name=1), T(name=0), T(name=3)])

Peter



More information about the Python-list mailing list