pyc创建时随机状态的变化? [英] change of random state when pyc created??

查看:62
本文介绍了pyc创建时随机状态的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很奇怪,但确实如此。

如果删除.pyc文件,我的程序会以不同的状态执行!


在一个目录中我有

module1和module2。


module1从module2导入随机和MyClass。

module2不导入如果__name__ ==" __ main __":

random.seed(314)

main()


我从(Windows)shell执行module1.py.

我得到一个结果,让我们称之为result1。

我再次执行它。我得到另一个结果,比如result2。

一次又一次地运行它,我得到result2。


现在我删除module2.pyc。

我从shell执行module1.py.

我得到result1。

我再次执行它;我得到了result2。

从那时起我得到result2,

除非我再次删除module.pyc,

在这种情况下我再一次得到result1。


有人可以向我解释这个吗?


谢谢,

Alan Isaac

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

推荐答案

5月4日晚上10点48分,Alan Isaac < ais ... @ american.eduwrote:
On May 4, 10:48 pm, "Alan Isaac" <ais...@american.eduwrote:

这看起来很奇怪,但确实如此。

如果我删除了.pyc文件,我的程序以不同的状态执行!


在一个目录中我有

module1和module2。


module1从module2导入random和MyClass。

module2不会随机导入。


module1设置这样的种子::


if __name__ ==" __ main __":

random.seed(314)

main()


我从(Windows)shell执行module1.py.

我得到一个结果,让我们称之为result1。

我再次执行它。我得到另一个结果,比如result2。

一次又一次地运行它,我得到result2。


现在我删除module2.pyc。

我从shell执行module1.py.

我得到result1。

我再次执行它;我得到了result2。

从那时起我得到result2,

除非我再次删除module.pyc,

在这种情况下我再一次得到结果1.


有人可以向我解释这个吗?


谢谢,

Alan Isaac
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



我无法想象为什么会这样,而且我无法使用Microsoft Windows XP和Python 2.5重现

行为:


< module1.py>

导入模块2

导入随机


def main ():

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


if __name__ ==''__ main __'':

random.seed(314)

main()

< /module1.py>


< ; module2.py>

随机导入

打印" module2导入>

def aRandom():

返回random.randrange(1000000)

< /module2.py>

C:\Documents and Settings \DUSTAN \De sktop\apackage> module1.py

module2进口

196431

111465

2638

628136

234231

207699

546775

449804

633844

179171

C:\Documents and Settings \DUSTAN\Desktop\apackage> module1.py

module2导入

196431

111465

2638

628136

234231

207699

546775

449804

633844

179171


C:\Documents and Settings\DUSTAN\Desktop\apackage> module1.py

module2进口

196431

111465

2638

628136

234231

207699

546775
$ b b 449804

633844

179171

C:\ Documents and Settings\DUSTAN\Desktop\APackage> ; module1.py

module2导入

196 431

111465

2638

628136

234231

207699

546775

449804

633844

179171


我删除了module2.pyc吧在最后一次通话之前。

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.


我在两个完全不同的系统上记录了这种行为



(Win 2000和Win XP SP2),使用Python 2.5.1。


发生这种情况的两个模块,如前所述,


如果不应该发生,那就有一个错误。

我正在寻找潜在的解释,

因为我发现不太可能找到错误。


Alan Isaac
I have documented this behavior
on two completely different systems
(Win 2000 and Win XP SP2), using Python 2.5.1.

It two modules where this happens,
as described before.
If it should not happen, there is a bug.
I am looking for potential explanation,
since I realize that finding bugs is unlikely.

Alan Isaac


5月6日上午9:00,Alan Isaac < ais ... @ american.eduwrote:
On May 6, 9:00 am, "Alan Isaac" <ais...@american.eduwrote:

我在两个完全不同的系统上记录了这个行为



(Win 2000和Win XP SP2),使用Python 2.5.1。
I have documented this behavior
on two completely different systems
(Win 2000 and Win XP SP2), using Python 2.5.1.



你不能说你已经记录了你的行为

没有发布表现出所谓行为的文件。

You can''t say that you have "documented" the behaviour when you
haven''t published files that exhibit the alleged behaviour.


这篇关于pyc创建时随机状态的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆