为什么?数学.ceil在flask shell命令中返回整数?

2024-10-01 00:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

环境:

python: 2.7.5
flask = 0.12.2
flask-cors = 2.0.1
flask-script = 2.0.5

启动python shell:

^{pr2}$

math.ceil返回一个float,但是当我使用flask-scriptShell命令启动python Shell时,它返回一个整数:

$ ./bin/python manage.py shell
==========================================
Starting server at 2018-01-06 15:30:14
==========================================

In [1]: import math

In [2]: math.ceil(3.2)
Out[2]: 4

我知道python3的math.ceil将返回一个整数,但我使用python2,有谁有好主意?在

检查python版本:

In [1]: import sys

In [2]: print (sys.version)
2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

Tags: inimport命令flask环境sysscript整数
2条回答

我找到了根本原因,对于我们的flask应用程序,我们添加了future包。在

future is the missing compatibility layer between Python 2 and Python 3. It allows you to use a single, clean Python 3.x-compatible codebase to support both Python 2 and Python 3 with minimal overhead.

此包重写math.ceil方法,它返回一个整数:

In [5]: math.ceil.func_code
Out[5]: <code object ceil at 0x7fd74853cab0, file "/data/apps/ecoboost/eggs/future-0.16.0-py2.7.egg/future/backports/misc.py", line 31>

源代码如下:

^{pr2}$

虽然看起来4是个整数。flask脚本有可能在没有十进制展开的情况下打印它。试着跑步

math.ceil(3.2)/3

写在烧瓶上。如果工作正常,它将打印1.3333333333333333。如果它打印1,那么您应该尝试重新安装flask脚本。在

相关问题 更多 >