如何在Python中通过ref传递值?
基本上我使用的是应用程序的 C++ API,但没有其 python 访问的参考。 一个变量是通过 ref 传递的,就像这样:
GetPoint ( Point &p, Object obj )
那么我怎样才能翻译成Python呢? 是否有通过参考符号?
Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:
GetPoint ( Point &p, Object obj )
so how can I translate to Python? Is there a pass by ref symbol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 中没有传递引用符号。
只需修改传入的点,您的修改将从调用函数中可见。
...
是的,尽管 Iraimbilanja 有一个很好的观点。 绑定可能已更改调用以返回点而不是使用输出参数。
There is no pass by reference symbol in Python.
Just modify the passed in point, your modifications will be visible from the calling function.
...
Yes, though Iraimbilanja has a good point. The bindings may have changed the call to return the point rather than use an out parameter.
您的 Python 绑定很可能已将该签名变成:
甚至:
因此请查看绑定的文档或来源。
It's likely that your Python bindings have turned that signature into:
or even:
So look into the bindings' documentation or sources.
我非常确定 Python 会将引用的值传递给变量。 本文 可能比我解释得更好。
I'm pretty sure Python passes the value of the reference to a variable. This article can probably explain it better than I.
在 Python 中,对象始终作为引用传递。 因此包裹在对象中会产生类似的效果。
Objects are always passed as reference in Python. So wrapping up in object produces similar effect.
python 中没有按符号引用 - 正确的做法取决于您的 API。 要问自己的问题是谁拥有从 C++ 传递到 python 的对象。 有时,最简单的方法就是将对象复制到 python 对象中,但这可能并不总是最好的做法。
您可能对 boost.python http:// www.boost.org/doc/libs/1_38_0/libs/python/doc/index.html
There is not ref by symbol in python - the right thing to do depends on your API. The question to ask yourself is who owns the object passed from C++ to python. Sometimes, the easiest ting it just to copy the object into a python object, but that may not always be the best thing to do.
You may be interested in boost.python http://www.boost.org/doc/libs/1_38_0/libs/python/doc/index.html