# Is it worth introducing \`copy()\` into learnxiny?

**URL:** <https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965>\
**Category:** New to Julia\
**Tags:** copy\
**Created:** [February 24, 2021, 9:48pm UTC](https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965 "2021-02-24T21:48:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [February 24, 2021, 9:48pm UTC](https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965/1 "2021-02-24T21:48:10Z")

</div>

Many new users use: [Learn Julia in Y Minutes](https://learnxinyminutes.com/docs/julia/)  
New users coming from Matlab/Octave (like myself) may not realize that using `=` w/ arrays does not create a new copy.

```julia
a1 = [1:5;]
a2 = a1
a2[5] = a2[5] +40
a2 
a1
#
a1 = [1:5;]
a2 = copy(a1)
a2[5] = a2[5] +40
a2 
a1

```

This is @ChrisRackauckas [Gotcha #5](http://www.stochasticlifestyle.com/7-julia-gotchas-handle/): Views, Copy, and Deepcopy

Is this [worth adding](https://github.com/adambard/learnxinyminutes-docs/blob/master/julia.html.markdown)?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 24, 2021, 10:02pm UTC](https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965/2 "2021-02-24T22:02:21Z")

</div>

Sure–it’s an important part of Julia, but it’s probably worth being clear that this has nothing to do with arrays in particular; rather it is a property of how `=` works with _everything_ in Julia. In fairness, though, new users will often first experience this in the context of an array just because they are so widely used.

Something like:

```julia
# Assignment with `=` attaches a new label to the same value without copying
a = [1, 2, 3]
b = a
# Now `b` and `a` point to the same value, so changing one affects the other:
a[3] = 5
b[3] # => 5

# The `copy()` function can create a shallow copy of an array, dictionary,
# or other container
a = [1, 2, 3]
c = copy(a)
a[3] = 5
c[3] # => 3

```

might be a nice addition.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [February 24, 2021, 10:14pm UTC](https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965/3 "2021-02-24T22:14:28Z")

</div>

Thanks.  
Should this go under:

## 1. Primitive Datatypes and Operators

or

## 2. Variables and Collections

or somewhere else  
I’m reluctant to use my own judgement bc alot of ppl use this tutorial…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 24, 2021, 10:27pm UTC](https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965/4 "2021-02-24T22:27:02Z")

</div>

I’d say probably (2), but I’d be interested if other people have opinions too.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [February 25, 2021, 4:11am UTC](https://discourse.julialang.org/t/is-it-worth-introducing-copy-into-learnxiny/55965/5 "2021-02-25T04:11:39Z")

</div>

Put it under (2) for now, else I’ll forget.  
Tagged @rdeits & linked to here.  
If someone has a better idea, we can improve it later
