Python2.7中flask-script Shell里math.ceil为何返回整数?
Why does
math.ceil return integer in flask-script's shell but float in native Python 2.7 shell? The key reason here is that flask-script's shell command uses IPython as the interactive shell backend (instead of the standard CPython shell), and IPython has an output formatting feature that simplifies the display of float values that are whole numbers.
Let me break this down clearly:
- In the native Python 2.7 shell,
math.ceil(3.2)correctly returns a float4.0—this is the standard behavior defined by Python 2.x'smathlibrary. - When you run
./bin/python manage.py shell, flask-script launches an IPython session (you can spot this from theIn [1]:/Out[2]:prompt style). In Python 2 environments, IPython automatically formats float values that are equivalent to integers to display as plain integers (like4instead of4.0) for better readability.
But important to note: this is just a display trick! The actual return type is still a float. You can verify this yourself in the flask-script shell:
In [1]: import math In [2]: res = math.ceil(3.2) In [3]: res Out[3]: 4 In [4]: type(res) Out[4]: float
The underlying Python version (2.7.5) and math library behavior haven't changed—only how the result is presented to you in the interactive session.
内容的提问来源于stack exchange,提问作者pangpang
相关产品推荐
相关产品推荐

