gotcha or bug? random state reset on irrelevant import

John Machin sjmachin at lexicon.net
Tue Apr 24 21:46:57 EDT 2007


On 25/04/2007 11:22 AM, Alan Isaac wrote:
> Running test.py will print False.
> Is this expected/desirable?
> Thanks,
> Alan Isaac
> 
> 
> %%%%%%% test.py %%%%%%%%%%%
> from random import seed, getstate
> seed(217)
> x = getstate()
> from test2 import Trivial

It's not irrelevant; the test2 module has to be *executed* as part of 
the import. Yes, "class" is an executable statement. Just about every 
Python statement is executable. seed(314) is executed.

> y = getstate()
> print x == y
> 
> 
> %%%%%  test2.py  %%%%%%%%%%%%%
> from random import seed

Insert here:
print "before seed call"

> seed(314)

Insert here:
print "have just executed some relevant code"

> class Trivial:
>     pass

HTH,
John



More information about the Python-list mailing list