有什么区别? numpy.Array通过Ref

发布于 2025-01-23 07:52:01 字数 629 浏览 1 评论 0原文

为什么我们在这些NP array上有两种不同的行为?

    def pass_by_ref(A: np.array):
        A = np.ones((2,2))
        return A

    def pass_by_ref_sideEffect(A: np.array):
        A[0][0] = 2
        return A


    A = np.zeros((2,2))

    B = pass_by_ref(A)
    print("A =\n", A)
    print("B =\n", B)

    C = pass_by_ref_sideEffect(A)
    print("A =\n", A)
    print("C =\n", C)

输出:

    A =
     [[0. 0.]
     [0. 0.]]
    B =
     [[1. 1.]
     [1. 1.]]
    A =
     [[2. 0.]
     [0. 0.]]
    C =
     [[2. 0.]
     [0. 0.]]

为什么我们会对pass_by_ref_sideeffect而不是pass_by_ref产生副作用?

Why do we have two different behaviors on these np.array?

    def pass_by_ref(A: np.array):
        A = np.ones((2,2))
        return A

    def pass_by_ref_sideEffect(A: np.array):
        A[0][0] = 2
        return A


    A = np.zeros((2,2))

    B = pass_by_ref(A)
    print("A =\n", A)
    print("B =\n", B)

    C = pass_by_ref_sideEffect(A)
    print("A =\n", A)
    print("C =\n", C)

output:

    A =
     [[0. 0.]
     [0. 0.]]
    B =
     [[1. 1.]
     [1. 1.]]
    A =
     [[2. 0.]
     [0. 0.]]
    C =
     [[2. 0.]
     [0. 0.]]

Why we have side effect on A after pass_by_ref_sideEffect and not with pass_by_ref?

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

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

发布评论

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

评论(2

椵侞 2025-01-30 07:52:01

这与您在变量中传递的方式无关,与分配的工作方式有关。在pass_by_ref()此行a = np.ones((2,2))创建一个新数组并将其分配给本地名称a。原始数组对象仍然存在,但是a不再指它。在另一种情况下,您是通过使用a [0] [0] = 2将原始数组来操纵原始数组。

如果您想在第一种情况下具有副作用,则分配给类似的片段:

def pass_by_ref(A: np.array):
    A[:,:] = np.ones((2,2))
    return A

A = np.zeros((2,2))
B = pass_by_ref(A)

print(A)
[[1., 1.],
 [1., 1.]]

print(B)
[[1., 1.],
 [1., 1.]]

以下是一个示例,在不传递变量中的函数中演示这一点:

In [1]: import numpy as np

In [2]: A = np.zeros((2,2))

In [3]: B = A

In [4]: B
Out[4]:
array([[0., 0.],
       [0., 0.]])

In [5]: A[:,:] = np.ones((2,2))

In [6]: B
Out[6]:
array([[1., 1.],
       [1., 1.]])

This has nothing to do with how you are passing in the variable and everything to do with how assignment works. In pass_by_ref() this line A = np.ones((2,2)) creates a new array and assigns it to the local name A. The original array object still exists, but A no longer refers to it. In the other case you are manipulating the original array by assigning to an element of it with A[0][0] = 2.

If you want to have a side effect in the first case, then assign to a slice of A like this:

def pass_by_ref(A: np.array):
    A[:,:] = np.ones((2,2))
    return A

A = np.zeros((2,2))
B = pass_by_ref(A)

print(A)
[[1., 1.],
 [1., 1.]]

print(B)
[[1., 1.],
 [1., 1.]]

Here's an example that demonstrates this without passing variables into functions:

In [1]: import numpy as np

In [2]: A = np.zeros((2,2))

In [3]: B = A

In [4]: B
Out[4]:
array([[0., 0.],
       [0., 0.]])

In [5]: A[:,:] = np.ones((2,2))

In [6]: B
Out[6]:
array([[1., 1.],
       [1., 1.]])

酷炫老祖宗 2025-01-30 07:52:01

当Python调用该函数时,它将传递对象的引用。当您使用a [0] [0] = 2进行分配时,它将根据variable a指向的对象的地址取代它写。但是,如果您使用a = np.ones(((2,2)),将创建​​一个新数组并制作变量a指向它。

When Python calls the function, it passes the reference of the object. When you use A[0][0] = 2 for assignment, it will offset according to the address of the object pointed to by variable A, find the address to be write. But if you use A = np.ones((2, 2)), a new array will be created and make variable A points to it.

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