Python 中向后兼容的输入调用
我想知道是否有人对编写向后兼容的 input() 调用来检索文件路径有建议?
在Python 2.x中,raw_input对于像/path/to/file这样的输入工作得很好。在这种情况下,使用输入在 3.x 中工作正常,但在 2.x 中由于 eval 行为而出现问题。
一种解决方案是检查 Python 的版本,并根据版本将 input
或 raw_input
映射到新函数:
if sys.version_info[0] >= 3:
get_input = input
else:
get_input = raw_input
我确信有更好的方法尽管这样做。有人有什么建议吗?
I was wondering if anyone has suggestions for writing a backwards-compatible input() call for retrieving a filepath?
In Python 2.x, raw_input worked fine for input like /path/to/file. Using input works fine in this case for 3.x, but complains in 2.x because of the eval behavior.
One solution is to check the version of Python and, based on the version, map either input
or raw_input
to a new function:
if sys.version_info[0] >= 3:
get_input = input
else:
get_input = raw_input
I'm sure there is a better way to do this though. Anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于Python 2.x版本的
input()
本质上是无用的,你可以简单地用raw_input
覆盖它:一般来说,我不会尝试瞄准有效的代码使用 Python 2.x 和 3.x,而是以适用于 2.x 的方式编写代码,然后使用
2to3
脚本获得可工作的 3.x 版本。Since the Python 2.x version of
input()
is essentially useless, you can simply overwrite it byraw_input
:In general, I would not try to aim at code that works with both, Python 2.x and 3.x, but rather write your code in a way that it works on 2.x and you get a working 3.x version by using the
2to3
script.现在许多 Python 教育和培训项目中都会教授此代码。
通常一起教授:
第一行:将 Python 3.x
print()
函数导入 Python 2.7,因此print()
在两个版本的 Python 下表现相同。如果这由于旧的print "some content"
调用而破坏了您的代码,您可以关闭此行。第二行和第三行:将Python 2.7
raw_input()
设置为input()
,这样input()
可以在两个版本的Python下使用而不会出现问题。如果这是您希望包含在代码中的唯一兼容性修复程序,则可以单独使用它。Python.org 网站上提供了更多来自 __future__ 的导入,以解决其他语言兼容性问题。还有一个名为“six”的库,在处理其他问题时可以查找兼容性解决方案。
This code is taught in many Python education and training programs now.
Usually taught together:
First line: imports the Python 3.x
print()
function into Python 2.7 soprint()
behaves the same under both versions of Python. If this breaks your code due to olderprint "some content"
calls, you can leave this line off.Second and third lines: sets Python 2.7
raw_input()
toinput()
soinput()
can be used under both versions of Python without problems. This can be used all by itself if this is the only compatibility fix you wish to include in your code.There are more
from __future__
imports available on the Python.org site for other language compatibility issues. There is also a library called "six" that can be looked up for compatibility solutions when dealing with other issues.你处理它的方式很好。使用 sys 模块可能有更多类似的方法,但请记住,如果您的程序对字符串和文件执行的操作不仅仅是微不足道的,那么最好有两个版本的程序而不是向后兼容的 python3 程序。
The way you are handling it is just fine. There are probably more similar ways using the
sys
module, but just in keep in mind that if you are program is doing something more than trivial with strings and files, it is better to have two versions of your program instead of having a backwards compatible python3 program.您可以导入该函数:
不幸的是,尽管此方法需要通过
pip install future
进行外部依赖You could import the function:
Unfortunately though this method requires an external dependency via
pip install future