SWIG:从Python调用Go

huangapple go评论97阅读模式
英文:

SWIG: Calling Go from Python

问题

SWIG能够用于从Python调用Go函数吗?

我在SWIG中看到的所有Go示例似乎都是从Go调用C/C++函数。

英文:

Can SWIG be used to call Go functions from Python?

The Go examples I have seen in SWIG all seem to call C/C++ functions from Go.

答案1

得分: 1

Go编译器之一可以生成C代码(cgo)。我从未查看过它,但可能有一种方法可以使用SWIG从Python调用这些函数。

此外,还有Cython,它将一种形式的Python转换为C,因此可以通过将这两者与C作为粘合剂结合起来提供另一种更直接的路径,而无需使用SWIG。

在Python WM上,我被一个Cython用户告知,通过小心使用Cython可以生成不需要“魔术”运行时的C代码。他们的技巧是使用Cython的机制,例如cdef,使他们的Python更接近C。通过更改Cython源代码,然后迭代地查看Cython输出,他们说可以使Python运行时“消失”,并达到“纯粹”的C。这并不理想,但可能比没有好。

因此,您可能可以使用这些机制来包装已编译为C的Go函数。但是这种方法将限制您使用Cython,这可能是一种过于约束的方式。

英文:

One of the Go compilers produces C (cgo). I have never looked at it, but there may be a path to calling those functions from Python using SWIG.

Also their is Cython, which converts a form-of-Python to C, so that may provide another more direct path by combining the two with C as the glue, and without SWIG.

I was told by a Cython user at Python WM, that with care, Cython can produce C which needs no 'magic' run-time to work. Their technique is to use Cython mechanisms e.g.cdef, to make their Python closer to C. By changing the Cython source then iteratively looking at the Cython output, they said they can cause Python run-time to 'disappear', and reach 'pure' C. This is not ideal, but might be better than nothing.

So you might be able to use that mechanisms to wrap around Go functions which have been compiled to C. BUT this approach would restrict you to using Cython, which might be too much of a constraint.

huangapple
  • 本文由 发表于 2012年3月31日 22:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/9956847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定