如何将python源代码转换为C++源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15303107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:43:52  来源:igfitidea点击:

How to convert python source code to C++ source code

pythonc++code-translation

提问by quandrei

I have been trying to find a way to convert .py source file to .cpp source (as a time saver from doing it manually). I've never used python before and was hoping for a quick way to convert it, and cleanup any code the converter might not do well.

我一直试图找到一种方法将 .py 源文件转换为 .cpp 源文件(作为手动操作的节省时间)。我以前从未使用过 python,并希望有一种快速的方法来转换它,并清理转换器可能做得不好的任何代码。

So far, some of the optionsthat I have found while googling seem to be: nuitka, cython, and pypy/rpython.

到目前为止,我在谷歌搜索时发现的一些选项似乎是:nuitka、cython 和 pypy/rpython。

However, the documentation I have read only seem to produce executables, and not actual source code.

但是,我阅读的文档似乎只生成可执行文件,而不是实际的源代码。

At this point, I have found py2c, but cannot seem to find any documentation on how to use it. Also, judging by the posted roadmap on the wiki, it does not seem to be a finished product, and so I'm doubtful as to its reliability.

在这一点上,我找到了 py2c,但似乎找不到任何关于如何使用它的文档。另外,从维基上公布的路线图来看,它似乎不是一个成品,所以我怀疑它的可靠性。

If you can provide other sources on how this can be accomplished, or shed some light on something I may have missed on the above-mentioned possibilities, it would be appreciated. Otherwise, I will simply convert it manually.

如果您可以提供有关如何实现这一点的其他来源,或者对我可能在上述可能性中遗漏的某些内容有所了解,我们将不胜感激。否则,我将简单地手动转换它。

采纳答案by Ned Batchelder

Programming languages cannot be easily converted like this. For example, Python has a large standard library, C++ doesn't have the same library. How do you translate those calls?

编程语言不能像这样轻松转换。例如,Python 有一个很大的标准库,C++ 没有相同的库。你如何翻译这些电话?

More fundamentally, the semantics of the language are very different, even a statement as simple as x = 1means something different in Python and C++.

更根本的是,语言的语义非常不同,即使是一个简单的语句, x = 1在 Python 和 C++ 中也意味着不同的东西。

You are going to have to write C++ while reading the Python.

在阅读 Python 时,您将不得不编写 C++。

回答by dugres

Have a look at shedskin, if it won't do the whole job,it still might be helpfull.

看看shedskin,如果它不能完成整个工作,它仍然可能会有所帮助。