为什么不可能将Python2与Python3混合使用? [英] Why is it impossible to mix Python2 with Python3?

查看:122
本文介绍了为什么不可能将Python2与Python3混合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管已经在Python2 vs Python3上进行了所有叙述和编写,但我无法确定为什么开发人员无法混合Python2和Python3代码.当然一定有原因吗?

Despite all that has been said and written on Python2 vs Python3, I have been unable to identify why the developers made it impossible to mix Python2 and Python3 code. Surely there must be a reason for this?

例如,在Fortran中,许多版本彼此不兼容,但是它们仍然可以在同一项目中愉快地共存.同样适用于C和C ++:某些C代码与C ++不兼容,但是编译器能够使用文件扩展名识别正确的语言.为什么没有为Python3选择这种方法有特定的原因吗?也就是说,让Python3模块以.py3扩展名(或shebang注释)标识,并为.py和.py3代码使用一个单一的解释器?

In Fortran, for instance, the many versions are incompatible with each other, but they can still happily co-exist within the same project. The same applies to C and C++: some C code is not compatible with C++, but the compiler is able to recognize the correct language using the file extension. Is there a specific reason for why this approach was not chosen for Python3? That is, let Python3 modules be identified by a .py3 extension (or a shebang comment), and use one single interpreter for both .py and .py3 code?

已经有一个名为为什么Python 3不向后兼容?,但是这个问题有所不同.我知道Python 3引入了新功能,因此打破了向后兼容性.这仍然并不意味着Python 2和3无法以相同的方式共存C和C ++.

There is already a question named Why is Python 3 not backwards compatible? , but this question is different. I know that Python 3 introduces new features and breaks backwards compatibility because of this. It still does not mean that Python 2 and 3 cannot coexist the same way C and C++ can.

推荐答案

您不能在同一项目中混合使用python2和python3,因为:

You can't mix python2 and python3 in the same project, because:

  1. 口译员是不同的.您正在为python2或python3运行解释器.我不知道当前有任何动态选择python2或python3运行时的解释器.
  2. 语法(略有不同).
  3. 类型不同.

但是,您当然可以同时运行python2和python3运行时,并在它们之间使用某种(IPC)消息传递.

However, you could certainly run both the python2 and python3 runtimes and use some sort of (IPC) message passing between them.

对于C和C ++,您可以在同一进程中运行它们,所以很好.偶然地,您也可以在同一进程中运行python和C(或C ++).

With the case of C, and C++, you can run them in the same process, so that's fine. Incidentially, you can run python and C (or C++) in the same process, too.

我想到的唯一允许您在同一进程中运行python2和python3的方法是将两个运行时都嵌入到同一进程中,但是,它们很可能会破坏彼此的全局变量并引起混乱.

The only way I can think of that would allow you to run python2 and python3 in the same process would be to embed both runtimes in the same process, however, they will very likely clobber each other's globals and get confused.

这篇关于为什么不可能将Python2与Python3混合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆