使用 = 运算符在 Julia 中创建副本 [英] Creating copies in Julia with = operator

查看:15
本文介绍了使用 = 运算符在 Julia 中创建副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

When I create some array A and assign it to B

A = [1:10]
B = A

I can modify A and the change reflects in B

A[1] = 42
# B[1] is now 42

But if I do that with scalar variables, the change doesn't propagate:

a = 1
b = a
a = 2
# b remains being 1

I can even mix the things up and transform the vector to a scalar, and the change doesn't propagate:

A = [1:10]
B = A
A = 0
# B remains being 1,2,...,10

What exactly does the = operator does? When I want to copy variables and modify the old ones preserving the integrity of the new variables, when should I use b = copy(a) over just b=a?

解决方案

The confusion stems from this: assignment and mutation are not the same thing.

Assignment. Assignment looks like x = ... – what's left of the = is an identifier, i.e. a variable name. Assignment changes which object the variable x refers to (this is called a variable binding). It does not mutate any objects at all.

Mutation. There are two typical ways to mutate something in Julia: x.f = ... – what's left of the = is a field access expression; x[i] = ... – what's left of the = is an indexing expression. Currently, field mutation is fundamental – that syntax can only mean that you are mutating a structure by changing its field. This may change. Array mutation syntax is not fundamental – x[i] = y means setindex!(x, y, i) and you can either add methods to setindex! or locally change which generic function setindex!. Actual array assignment is a builtin – a function implemented in C (and for which we know how to generate corresponding LLVM code).

Mutation changes the values of objects; it doesn't change any variable bindings. After doing either of the above, the variable x still refers to the same object it did before; that object may have different contents, however. In particular, if that object is accessible from some other scope – say the function that called one doing the mutation – then the changed value will be visible there. But no bindings have changed – all bindings in all scopes still refer to the same objects.

You'll note that in this explanation I never once talked about mutability or immutability. That's because it has nothing to do with any of this – mutable and immutable objects have exactly the same semantics when it comes to assignment, argument passing, etc. The only difference is that if you try to do x.f = ... when x is immutable, you will get an error.

这篇关于使用 = 运算符在 Julia 中创建副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆