change of random state when pyc created??

Dustan DustanGroups at gmail.com
Sat May 5 14:03:06 EDT 2007


On May 4, 10:48 pm, "Alan Isaac" <ais... at american.edu> wrote:
> This may seem very strange, but it is true.
> If I delete a .pyc file, my program executes with a different state!
>
> In a single directory I have
> module1 and module2.
>
> module1 imports random and MyClass from module2.
> module2 does not import random.
>
> module1 sets a seed like this::
>
> if __name__ == "__main__":
>     random.seed(314)
>     main()
>
> I execute module1.py from the (Windows) shell.
> I get a result, let's call it result1.
> I execute it again. I get another result, say result2.
> Running it again and again, I get result2.
>
> Now I delete module2.pyc.
> I execute module1.py from the shell.
> I get result1.
> I execute it again; I get result2.
> From then on I get result2,
> unless I delete module.pyc again,
> in which case I once again get result1.
>
> Can someone explain this to me?
>
> Thank you,
> Alan Isaac

I can't imagine why that would be, and I was unable to reproduce that
behavior, using Microsoft Windows XP and Python 2.5:

<module1.py>
import module2
import random

def main():
    for i in range(10): print module2.aRandom()

if __name__ == '__main__':
    random.seed(314)
    main()
</module1.py>

<module2.py>
import random
print "module2 imported"

def aRandom():
    return random.randrange(1000000)
</module2.py>


C:\Documents and Settings\DUSTAN\Desktop\apackage>module1.py
module2 imported
196431
111465
2638
628136
234231
207699
546775
449804
633844
179171

C:\Documents and Settings\DUSTAN\Desktop\apackage>module1.py
module2 imported
196431
111465
2638
628136
234231
207699
546775
449804
633844
179171

C:\Documents and Settings\DUSTAN\Desktop\apackage>module1.py
module2 imported
196431
111465
2638
628136
234231
207699
546775
449804
633844
179171

C:\Documents and Settings\DUSTAN\Desktop\apackage>module1.py
module2 imported
196431
111465
2638
628136
234231
207699
546775
449804
633844
179171

I deleted module2.pyc right before that last call.




More information about the Python-list mailing list