python from __future__ import division

来源:互联网 发布:苹果电脑不能淘宝付款 编辑:程序博客网 时间:2024/06/06 02:17
from __future__ import division
导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精确除法之后,"/"执行的是精确除法,如下所示:
---------------------------------------------------------------------------------------------
>>> 3/4
0
>>> from __future__ import division
>>> 3/4

0.75

--------------------------------------------------------------------------------------------

导入精确除法后,若要执行截断除法,可以使用"//"操作符:
--------------------------------------------------------------------------------------------
>>> 3//4
0
>>> 
--------------------------------------------------------------------------------------------
一些将来特征如下:
featureoptional inmandatory ineffectnested_scopes2.1.0b12.2PEP 227Statically Nested Scopesgenerators2.2.0a12.3PEP 255Simple Generatorsdivision2.2.0a23.0PEP 238Changing the Division Operatorabsolute_import2.5.0a12.7PEP 328Imports: Multi-Line and Absolute/Relativewith_statement2.5.0a12.6PEP 343The “with” Statementprint_function2.6.0a23.0PEP 3105Make print a functionunicode_literals2.6.0a23.0PEP 3112Bytes literals in Python 3000
PEP:Python Enhancement Proposals
可以在这个地方找到很多PEP:http://www.python.org/dev/peps/ 里面还能看到许多提议的动机
比如有division:
The current division (/) operator has an ambiguous meaning for numerical arguments: 
it returns the floor of the mathematical  result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex.  
This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.
原创粉丝点击