Python如何处理“来自__future__ import division”?

时间:2016-07-31 18:26:13

标签: python

我看过source code for __future__.py对我来说没有意义 - 这实际上是如何改变分裂的行为的?

1 个答案:

答案 0 :(得分:4)

该模块仅用于纪录片/内省目的;其中没有任何代码实际上 任何东西。

相反,当Python编译模块时,它会调用模块上的PyFuture_FromAST,它会检查from __future__ import语句,并假设它们有效,sets the appropriate flags在{{1}上对象。然后编译器继续前进sets those flags in the compiler context,然后继续编译模块。

为了进行比较,您可以看到在Python 3中,PyFutureFeatures仍然相同且包含所有相同的信息,但在__future__.pynone of the features actually set any flags中,因为所有这些功能都是在Python 3中默认启用。