# What is the philosophy behind arrays not being copied when assigned to another variable?

**URL:** <https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702>\
**Category:** New to Julia\
**Created:** [May 11, 2023, 10:24pm UTC](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702 "2023-05-11T22:24:13Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 13, 2023, 1:39am UTC](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702/23 "2023-05-13T01:39:43Z")

</div>

Oh, 100% - no shade at all intended 🙂

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 13, 2023, 3:00am UTC](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702/24 "2023-05-13T03:00:08Z")

</div>

Note there is another difference.

Slicing, or range indexing, in numpy creates a view.

```julia
In [1]: import numpy as np
In [2]: a = np.zeros(5)
In [3]: a[0] = 3
In [4]: b = a[0:2]
In [5]: b[0] = 5
In [6]: a[0]
Out[6]: 5.0

```

Slicing in Julia creates a copy.

```julia
julia> a = zeros(5)
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> b = a[1:2]
2-element Vector{Float64}:        
 0.0
 0.0

julia> b[1] = 5
5

julia> a[1]
0.0

```

Unless one explicitly creates a view in Julia.

```julia
julia> c[1] = 7                   
7     
                            
julia> a[1]                       
 7.0

```

> <https://github.com/JuliaLang/julia/issues/3701>
>
> I would prefer that range indexing of an \`Array\` (e.g. \`X\[1:10, :\]\`) created a \`…SubArray\` (a view/reference), not a copy. This seems more in the spirit of Julia's pass-by-reference semantics, and would eliminate some of the performance gotchas with range indexing.
> 
> It might also make future loop-devectorization optimization easier, because subarray references can be devectorized back into references to the original array without worrying that you will be changing the semantics.
> 
> It would reduce Matlab compatibility, but we already do that with pass-by-reference and so I doubt many users will be surprised by having the same reference semantics for range indexing.
> 
> This is something that has come up informally several times on the mailing list, but I didn't see any issue for it.

Is this still consistent?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 13, 2023, 4:30am UTC](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702/25 "2023-05-13T04:30:34Z")

</div>

> [@Henrique\_Becker](#):
>
> Thanks, but I believe @kevbonham just chose a word with a negative baggage

I know. I was just making fun of an obvious typo😄

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 13, 2023, 8:46am UTC](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702/26 "2023-05-13T08:46:56Z")

</div>

> [@Henrique\_Becker](#):
>
> a cat alternates between living two houses, in one of them it is called Tiger, in the other it is called Fluffy, the cat is the same, and it does not know any of its names.

My cat approves of this example, as do I. 😸

> **code**
>
> ```julia
> julia-1.9> a_cat = split("""
> .......................................
> .......................................
> ...........................䷀䷀..........
> ............................䷀䷀.........
> .............................䷀.........
> .............................䷀䷀........
> ..........䷀䷀.................䷀䷀........
> ..........䷀䷀.................䷀䷀........
> .........䷀䷀䷀䷀................䷀.........
> ........䷀䷀䷀䷀䷀䷀...............䷀.........
> ........䷀䷀䷀䷀䷀䷀䷀.............䷀䷀.........
> ..........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀..........
> ...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀..........
> ...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........
> ...........䷆䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........
> ............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........
> ............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........
> .............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........
> .............䷀䷀䷀䷀䷀.....䷀䷀䷀䷀䷀䷀䷀䷀........
> .............䷀䷀.䷀䷀.....䷀䷀䷀.䷀䷀䷀䷀........
> .............䷀..䷀䷀.....䷀䷀....䷀䷀........
> ............䷀䷀...䷀.....䷀䷀.....䷀........
> ............䷀....䷀䷀...䷀䷀......䷀........
> ...........䷀䷀.....䷀..䷀䷀......䷀䷀........
> .......................................
> .......................................""");
> 
> julia-1.9> tiger = a_cat;
> 
> julia-1.9> fluffy = a_cat
> 26-element Vector{SubString{String}}:
> "......................................."
> "......................................."
> "...........................䷀䷀.........."
> "............................䷀䷀........."
> ".............................䷀........."
> ".............................䷀䷀........"
> "..........䷀䷀.................䷀䷀........"
> "..........䷀䷀.................䷀䷀........"
> ".........䷀䷀䷀䷀................䷀........."
> "........䷀䷀䷀䷀䷀䷀...............䷀........."
> "........䷀䷀䷀䷀䷀䷀䷀.............䷀䷀........."
> "..........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........."
> "...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........."
> "...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷆䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> ".............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> ".............䷀䷀䷀䷀䷀.....䷀䷀䷀䷀䷀䷀䷀䷀........"
> ".............䷀䷀.䷀䷀.....䷀䷀䷀.䷀䷀䷀䷀........"
> ".............䷀..䷀䷀.....䷀䷀....䷀䷀........"
> "............䷀䷀...䷀.....䷀䷀.....䷀........"
> "............䷀....䷀䷀...䷀䷀......䷀........"
> "...........䷀䷀.....䷀..䷀䷀......䷀䷀........"
> "......................................."
> "......................................."
> 
> julia-1.9> tiger == fluffy
> true
> 
> julia-1.9> reverse!(tiger)
> 26-element Vector{SubString{String}}:
> "......................................."
> "......................................."
> "...........䷀䷀.....䷀..䷀䷀......䷀䷀........"
> "............䷀....䷀䷀...䷀䷀......䷀........"
> "............䷀䷀...䷀.....䷀䷀.....䷀........"
> ".............䷀..䷀䷀.....䷀䷀....䷀䷀........"
> ".............䷀䷀.䷀䷀.....䷀䷀䷀.䷀䷀䷀䷀........"
> ".............䷀䷀䷀䷀䷀.....䷀䷀䷀䷀䷀䷀䷀䷀........"
> ".............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷆䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........."
> "..........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........."
> "........䷀䷀䷀䷀䷀䷀䷀.............䷀䷀........."
> "........䷀䷀䷀䷀䷀䷀...............䷀........."
> ".........䷀䷀䷀䷀................䷀........."
> "..........䷀䷀.................䷀䷀........"
> "..........䷀䷀.................䷀䷀........"
> ".............................䷀䷀........"
> ".............................䷀........."
> "............................䷀䷀........."
> "...........................䷀䷀.........."
> "......................................."
> "......................................."
> 
> julia-1.9> fluffy
> 26-element Vector{SubString{String}}:
> "......................................."
> "......................................."
> "...........䷀䷀.....䷀..䷀䷀......䷀䷀........"
> "............䷀....䷀䷀...䷀䷀......䷀........"
> "............䷀䷀...䷀.....䷀䷀.....䷀........"
> ".............䷀..䷀䷀.....䷀䷀....䷀䷀........"
> ".............䷀䷀.䷀䷀.....䷀䷀䷀.䷀䷀䷀䷀........"
> ".............䷀䷀䷀䷀䷀.....䷀䷀䷀䷀䷀䷀䷀䷀........"
> ".............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "............䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷆䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀........."
> "...........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........."
> "..........䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀䷀.........."
> "........䷀䷀䷀䷀䷀䷀䷀.............䷀䷀........."
> "........䷀䷀䷀䷀䷀䷀...............䷀........."
> ".........䷀䷀䷀䷀................䷀........."
> "..........䷀䷀.................䷀䷀........"
> "..........䷀䷀.................䷀䷀........"
> ".............................䷀䷀........"
> ".............................䷀........."
> "............................䷀䷀........."
> "...........................䷀䷀.........."
> "......................................."
> "......................................."
> 
> ```

---

<div class="post-metadata">

**Author:** ![stepanzh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stepanzh/32/34851_2.png) [@stepanzh](https://discourse.julialang.org/u/stepanzh)\
**Post date:** [May 19, 2023, 4:12pm UTC](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702/28 "2023-05-19T16:12:30Z")

</div>

Perhaps, out of the topic, but

> Why would I have created `b` in the first place?

I often use assignment to “unpack” structs or to keep simplicity of both usage and developing of a function. For the latter, I prefer to give clear names of the arguments to a user. But, for the developer, the names may be a bit crypty, e.g. short. Julia’s and Python’s behavior of assignment helps me create interfaces comfortable for both “sides”, also Swift [syntactically supports](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions#Function-Argument-Labels-and-Parameter-Names) this.

A short example

```julia
function optimize(target_function, initial_guess)
   f = target_function
   x0 = initial_guess
   ...
end

```

[Previous page](https://discourse.julialang.org/t/what-is-the-philosophy-behind-arrays-not-being-copied-when-assigned-to-another-variable/98702.md?page=1)
