Python __future__ 方法

发布于 2024-10-09 17:27:58 字数 144 浏览 0 评论 0原文

这样说是否正确

__future__.__dict__.keys() 

如果“print”没有被列为方法之一,那么我使用的Python版本不提供未来的打印功能, ? (我使用的是Python 2.5.5。)

Would it be correct to say that if 'print' is not listed as one of the methods in

__future__.__dict__.keys() 

then the version of Python that I am using does not supply the future print function? (I am using Python 2.5.5.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲸落 2024-10-16 17:27:58

是的,但这就是 print_function,它是在 Python 2.6 中引入的。

Right, but that would be print_function, and that was introduced in Python 2.6.

情泪▽动烟 2024-10-16 17:27:58

几乎正确。该功能称为 print_function,而不是打印。

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import __future__
>>> __future__.print_function
_Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 65536)

Almost correct. The feature is called print_function, not print.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import __future__
>>> __future__.print_function
_Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 65536)
祁梦 2024-10-16 17:27:58

是的。这是正确的。 但进行该检查的更好方法是:


尝试:
未来导入 print_function
除了导入错误:
print "print 是一个 stmt"

您不能执​​行上述导入和检查 print_function 的方法。可用的方法似乎是,检查 sys.version 并检查 __future__ 字典,就像你正在做的那样。

Yes. That is correct. But a better way to do that check would be:


try:
from future import print_function
except ImportError:
print "print is a stmt"

You cannot do the above way of importing and checking for print_function. The ways available seems, checking with sys.version and checking the __future__ dictionary as you are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文