我了解到Python 3不向后兼容.
它会不会影响使用旧版Python的很多应用程序?
Python 3的开发人员怎么不认为使其向后兼容是绝对必要的?
我将应用程序从python 2移植到python 3,遇到以下问题:random.randint根据使用的Python版本返回不同的结果。所以
import random
random.seed(1)
result = random.randint(1, 100)
Run Code Online (Sandbox Code Playgroud)
在Python 2.x上,结果将为14,在Python 3.x上:18
不幸的是,我需要在python3上具有相同的输出才能具有服务的向后兼容性。
现在我只有subprocess从Python 3.x 使用模块来执行Python 2.x代码的工作思路
result = subprocess.check_output(
'''python2 -c "import random; random.seed('%s'); print(random.randint(1, 100))"''' % seed,
shell=True
)
Run Code Online (Sandbox Code Playgroud)
但是这种方法大约慢一些。比执行仅仅1000次random.randint(1, 100)。
也许还有其他方法可以做到这一点?