【发布时间】:2013-08-08 00:38:52
【问题描述】:
我在使用 Python 2.7 的 Google-App-Engine 项目中使用了一些用 Python 3 编写的旧代码。 Python 3 和 Python 2 中不同的 round() 算法让我很头疼。在 Python 2.7 中实现 Python 3 的 round() 方法有什么方便的方法吗?
另一个问题:Python 2 和 Python 3 处理整数运算的方式完全不同。例如,以下语句在 Python 2 和 3 中具有不同的输出:
2/4 # 0 in Python 2, 0.5 in Python 3
round(3/2)
math.ceil(0.5) # 1.0 in Python 2, 1 in Python 3
有什么简单的方法可以将代码从 Python 3 转换为 Python 2,同时保持行为完全相同?
谢谢!
【问题讨论】:
-
在py2.x中使用
from __future__ import division获取py3.x的除法行为。
标签: python python-2.7 python-3.x google-app-engine rounding