Python future模块
2017-05-07 21:31
459 查看
Python编程语言中有很多比较有用的模块,这些模块在实际使用中可以帮助我们轻松的完成许多功能需求。我们今天将会在这里通过对Python future模块的了解,来对这方面的知识做一个详细的了解。
今天在学习Python Cookbook的时候,发现一句语法from future import division,很奇怪future这个名字,网上搜了一下,原来是很有用的一个模块。
详细说明见这里。按照官方的解释,至少确保在2.1之前版本的Python可以正常运行一些新的语言特性,需要使用语句
‘from future import *’。举例来说:
Enable nested scopes in Python2.1
from future import nested_scopes
如果使用这个语句,则该语句必须是模块或程序的第一个语句。此外,’_ future_’ 模块中存在的特性最终将成为Python语言标准的一部分。到那时,将不再需要使用Python future模块。
Python future模块的更多示例:
1. Python 2.6中也有一个 future import 使得所有的字符串文本成为Unicode字符串。这就意味着\u转义序列可以用于包含Unicode字符。
from future import unicode_literals
s = (‘\u751f\u3080\u304e\u3000\u751f\u3054’
‘\u3081\u3000\u751f\u305f\u307e\u3054’)
print len(s) # 12 Unicode characters
2. Python 2.6可以通过 import future 来将print从语言语法中移除,让你可以使用函数的形式。例如:
from future import print_function
print(‘# of entries’, len(dictionary), file=sys.stderr)
3. 整数除法
python 2.5中:23/6 # 得3
from future import division 之后:
23/6 # 得 3.8333333333333335
参考:http://developer.51cto.com/art/201003/186805.htm
今天在学习Python Cookbook的时候,发现一句语法from future import division,很奇怪future这个名字,网上搜了一下,原来是很有用的一个模块。
详细说明见这里。按照官方的解释,至少确保在2.1之前版本的Python可以正常运行一些新的语言特性,需要使用语句
‘from future import *’。举例来说:
Enable nested scopes in Python2.1
from future import nested_scopes
如果使用这个语句,则该语句必须是模块或程序的第一个语句。此外,’_ future_’ 模块中存在的特性最终将成为Python语言标准的一部分。到那时,将不再需要使用Python future模块。
Python future模块的更多示例:
1. Python 2.6中也有一个 future import 使得所有的字符串文本成为Unicode字符串。这就意味着\u转义序列可以用于包含Unicode字符。
from future import unicode_literals
s = (‘\u751f\u3080\u304e\u3000\u751f\u3054’
‘\u3081\u3000\u751f\u305f\u307e\u3054’)
print len(s) # 12 Unicode characters
2. Python 2.6可以通过 import future 来将print从语言语法中移除,让你可以使用函数的形式。例如:
from future import print_function
print(‘# of entries’, len(dictionary), file=sys.stderr)
3. 整数除法
python 2.5中:23/6 # 得3
from future import division 之后:
23/6 # 得 3.8333333333333335
参考:http://developer.51cto.com/art/201003/186805.htm
相关文章推荐
- python模块之__future__模块
- python的__future__模块
- Python __future__ 模块
- python 中的__future__模块
- Python标准模块——__future__
- 用Python2编译Python3.X的代码: __future__ 模块
- 关于python 中的__future__模块(from __future__ import ***)
- Python: __future__ 模块
- python from _future_模块
- python 的 __future__ 模块
- Python __future__模块常见示例相关解读
- python中__future__模块
- python模块学习之__future__
- python——模块——__future__
- Python: __future__模块
- 关于python中的__future__模块
- Python之__future__模块
- Python中的__future__模块
- 介绍Python中的__future__模块
- Python模块之 __future__