簡體   English   中英

如何告訴新的Python使用舊的打印

[英]How can I tell new Python to use the old print

打印功能的語法在python的較新版本中已更改。 問題是在家里,我有舊版本的python,而在辦公室里卻有。 如何在新的和舊的python版本上運行相同的程序?

如果您使用的是iPython / Jupyter,請運行

%autocall 1

之后,您可以使用

print "yo"

就像在Python 2中一樣。

在此很好地介紹了許多2 vs 3移植問題。

總的來說,我認為嘗試保留一個在兩個版本下都可以運行的.py文件要比花錢麻煩。 但是,如果您想嘗試,該鏈接將顯示可用的解決方法。

例如,雖然Python 2確實支持,

print("abc", 3)

結果不一樣:在Python 2中,打印出元組 ('abc', 3) 而且Python 2的print完全不支持Python 3的可選關鍵字參數(例如file= )。

您可以在舊版本的python上使用新的打印語法。

您可以在舊腳本中使用新的print功能,如下所示:

# This line goes at the top of the file ...
from __future__ import print_function

# And then you can use the new-style print function anywhere
print("hello")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM