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

时间:2018-01-06 07:32:52

标签: python python-2.7 flask ceil flask-script

ENV:

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

启动一个python shell:

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.ceil(3.2)
4.0

math.ceil返回一个浮点数,但当我使用flask-script Shell命令启动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)]

2 个答案:

答案 0 :(得分:0)

虽然看起来4是一个整数。烧瓶脚本可能在没有十进制扩展的情况下打印它。尝试运行

math.ceil(3.2)/3
烧瓶脚本中的

。如果它正常工作,它将打印1.3333333333333333。如果它打印1,那么你应该尝试重新安装flask-script。

答案 1 :(得分:0)

我找到了根本原因,对于我们的烧瓶应用,我们添加了future包。

  

未来是Python 2和Python 3之间缺少的兼容性层。它允许您使用单个,干净的Python 3.x兼容代码库,以最小的开销支持Python 2和Python 3.

这个包重写了 public class Patientinfo { private String queuenumber,appointmenttime,patientinitials,arrivaltime; public Patientinfo{ } public Patientinfo(String queunumber,String appointmenttime,String patientinitials,String arrivaltime) { this.queunumber=queunumber; this.appointmenttime=appointmenttime; this.patientinitials=patientinitials; this.arrivaltime=arrivaltime; } public String getQueuenumber() { return queuenumber; } public String getArrivaltime() { return arrivaltime; } public void setArrivaltime() { this.arrivaltime=arrivaltime; } public String getAppointmenttime() { return appointmenttime; } public String getPatientinitials() { return patientinitials; } public void setPatientinitials(String patientinitials) { this.patientinitials=patientinitials; } public void setAppointmenttime(String appointmenttime) { this.appointmenttime=appointmenttime; } public void setQueuenumber(String queuenumber) { this.queuenumber=queuenumber; } } 方法,它返回一个整数:

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>