change of random state when pyc created??

John Machin sjmachin at lexicon.net
Sun May 6 08:16:39 EDT 2007


On May 6, 9:41 pm, Dustan <DustanGro... at gmail.com> wrote:
> On May 6, 1:00 am, Steven D'Aprano
>
>
>
> <s... at REMOVE.THIS.cybersource.com.au> wrote:
> > On Sun, 06 May 2007 00:20:04 +0000, Alan Isaac wrote:
> > >> Should you not expect to get the same result each time? Is that not
> > >> the point of setting a constant seed each time you run the script?
>
> > > Yes.  That is the problem.
> > > If I delete module2.pyc,
> > > I do not get the same result.
>
> > I think you have missed what John Machin is pointing out. According to
> > your original description, you get different results even if you DON'T
> > delete module2.pyc.
>
> > According to your original post, you get the _same_ behaviour the first
> > time you run the script, regardless of the pyc file being deleted or not.
>
> > You wrote:
>
> > [quote]
> > 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.
> > [end quote]
>
> > So, with module2.pyc file existing, you get result1 the first time you
> > execute module1.py, and then you get result2 every time from then onwards.
>
> Umm... no.
>
> module2.pyc is created by the first run.
>

Yes, I've realised that too.

Some news:
(1) Alan has sent Dustan and me a second smaller version of the two
files.
I'll forward them on to Steven -- they're now called test.py and
test1.py, but I'll continue to call them module[12].py
(2) The problem is definitely reproducible -- whether or not
module2.pyc has been deleted or retained from the previous run is
affecting the results. [Windows XP SP2; Python 2.5.1]
(3) module2.py appears to me not to be guilty of causing any changes
in state; it contains only rather inocuous functions and classes.
(4) I have put
print random.getstate()
before and after the call to main() in the executed script
(module1.py). Diffing the stdout of a no-pyc run and a with-pyc run
shows differences in Alan's output but NO DIFFERENCES in either the
"before" or the "after" random.getstate() output. Looks like the
problem is nothing to do with the random module.
(5) I have backported the files to Python 2.4 by replacing the use of
defaultdict in module2.py with explicit "if key in the_dict" code --
now the problem is reproducible with Python 2.4.3 as well as with
2.5.1.
(6) I've changed the 'from module2 import foo, bar, zot; foo()" to use
the "import module2; module2.foo()" style -- no effect; still has the
problem.

Cheers,
John




More information about the Python-list mailing list