Python:引用变量(hack)

发布于 2024-12-07 08:58:47 字数 504 浏览 2 评论 0原文

有没有什么方法(hack)可以推动Python函数(def)通过引用返回结果,即使对于不可变类型也是如此?

提案申请(交换为子程序):
def 交换(a, b):
.....a,b = b,a

注意:
def 交换(a, b):
.....返回b,a

作为函数工作,不是问题的答案!


例如,有一个就地工作的函数 random.shuffle(a) 。 我的想法是调用用 Fortran/C++ 编写的函数并通过 Python 调用它们。它确实有效,但也有缺点。
注意:
lambda”和“def”(作为函数)都存在以下问题:a, b = swap(a, b),这需要关心变量的顺序。在我的建议中(如果可能的话),子例程被用作:swap(a, b)因此不需要关心变量的顺序

Is there any way (hack) to push Python function (def) to return results by reference even for immutable types?

A proposal application (swap as subroutine):
def swap(a, b):
.....a,b = b,a

Note:
def swap(a, b):
.....return b,a


works as function which is not the answer of the question!


For example there is a function random.shuffle(a) that works in-place.
My idea is to call a function written in Fortran/C++ and call them via Python. It does work but has disadvantages too.

note:
Both "lambda" and "def" (as function) have the following problem: a, b = swap(a, b) which requires care about order of variables. In my proposal (if it was possible) the subroutine is used as: swap(a, b) so there is no requirement to care about order of variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一片旧的回忆 2024-12-14 08:58:47

Python 中的所有名称都是引用。不,没有可用的“out”引用(例如,在C++意义上)。您需要传递一个可变对象,然后可以在函数中改变它。但话又说回来,返回新值应该是首选方式。

All names in Python are references. And no, there are no "out" references (e.g. in a C++ sense) available. You need to pass a mutable object and then you can mutate it in the function. But then again, returning new value(s) should be the preferred way.

将军与妓 2024-12-14 08:58:47

不,这样的事情不存在,因为你获取给定的对象作为引用,但如果你重新分配它,它不会被改回来。

在这种情况下,您要么必须使用可变容器(列表、字典、带属性的对象)。

No, such things don't exist, because you get the given object as a reference, but if you re-assign it, it won't be changed back.

You either have to work with a mutable container (list, dict, object with attributes) in this case.

早茶月光 2024-12-14 08:58:47

在Python中没有办法传递“写指针”。您可以传递一个对象和一个名称(因此被调用者可以使用例如 setattr),也可以传递一个列表和一个索引。不存在上下文无关的“可修改单元格的地址”之类的东西......只有名称或索引,但也需要上下文(哪个名称空间,哪个数组)。

在一般情况下,如果确实需要传递写入指针,Python 中的解决方案是传递一个“setter”函数,被调用者可以使用该函数来设置值。

例如:

def foo(setter):
    setter(42)

def bar():
    x = [1,2,3,4]

    def setFirst(value):
        x[0] = value

    foo(setFirst)

    print x[0] # --> 42

In python there is no way to pass a "write pointer". You can pass an object and a name (so the callee can use for example setattr) or you can pass a list and an index. There is no such a thing as a context free "address of a modifiable cell"... there are only names or indexes that however also need a context (which namespace, which array).

In the general case if you really need to pass a write pointer a solution in Python is to pass a "setter" function that can be used by the callee to set the value.

For example:

def foo(setter):
    setter(42)

def bar():
    x = [1,2,3,4]

    def setFirst(value):
        x[0] = value

    foo(setFirst)

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