如何在Python中通过ref传递值?

发布于 2024-07-14 05:30:26 字数 176 浏览 9 评论 0原文

基本上我使用的是应用程序的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉城凉梦凉人心 2024-07-21 05:30:26

Python 中没有传递引用符号。

只需修改传入的点,您的修改将从调用函数中可见。

>>> def change(obj):
...     obj.x = 10
...
>>> class Point(object): x,y = 0,0
...
>>> p = Point()
>>> p.x
0
>>> change(p)
>>> p.x
10

...

所以我应该像这样传递它:GetPoint (p, obj)?

是的,尽管 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.

>>> def change(obj):
...     obj.x = 10
...
>>> class Point(object): x,y = 0,0
...
>>> p = Point()
>>> p.x
0
>>> change(p)
>>> p.x
10

...

So I should pass it like: GetPoint (p, obj)?

Yes, though Iraimbilanja has a good point. The bindings may have changed the call to return the point rather than use an out parameter.

酒儿 2024-07-21 05:30:26

您的 Python 绑定很可能已将该签名变成:

Point GetPoint(Object obj)

甚至:

Point Object::GetPoint()

因此请查看绑定的文档或来源。

It's likely that your Python bindings have turned that signature into:

Point GetPoint(Object obj)

or even:

Point Object::GetPoint()

So look into the bindings' documentation or sources.

孤蝉 2024-07-21 05:30:26

我非常确定 Python 会将引用的值传递给变量。 本文 可能比我解释得更好。

I'm pretty sure Python passes the value of the reference to a variable. This article can probably explain it better than I.

热风软妹 2024-07-21 05:30:26

在 Python 中,对象始终作为引用传递。 因此包裹在对象中会产生类似的效果。

Objects are always passed as reference in Python. So wrapping up in object produces similar effect.

若相惜即相离 2024-07-21 05:30:26

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文