Python中的__future__

在Python中,你如果在某一个版本的Python想使用未来版本中的功能,可以使用如下语法实现:

from __future__ import futurename

这条语句必须放在module文件的第一行才行。

 

比如想在Python 2.X中使用Python 3.X的print函数,那么,只需要在模块文件第一行加入:

from __future__ import print_function

 

但是,怎样才能知道futurename有哪些呢?一个方法如下:

import __future__   # 导入__future模块

dir(__future__)   # 列举出__future__模块中的属性
['with_statement', 'print_function', 'absolute_import '...]

 

posted @ 2018-07-22 17:33  chaoguo1234  阅读(250)  评论(0编辑  收藏  举报