# Behaviour of Ref?

**URL:** https://discourse.julialang.org/t/behaviour-of-ref/115321
**Category:** New to Julia
**Tags:** pycall, python
**Created:** [June 7, 2024, 11:33am UTC](https://discourse.julialang.org/t/behaviour-of-ref/115321 "2024-06-07T11:33:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [June 7, 2024, 11:33am UTC](https://discourse.julialang.org/t/behaviour-of-ref/115321/1 "2024-06-07T11:33:44Z")

</div>

Hello everybody,

I am trying to use Ref to change a variable, and it does not work as expected (at least, as I expected).

```julia
julia> a = 5
5

julia> typeof(a)
Int64

julia> b = Ref(a)
Base.RefValue{Int64}(5)

julia> typeof(b)
Base.RefValue{Int64}

julia> b[] = 10
10

julia> b
Base.RefValue{Int64}(10)

julia> a
5

```

I believed that changing b the variable a should be changed. I think previously it was working in that way, but now it does not.

I actually does not use directly Ref, but I try to change results of a package in Python that uses PyCall (pysr). In a previous version, I could change the internal structure (using small functions in Julia) but now it is not working now, it is like it was a copy. Studying that I come to this expected (for me) behaviour.

Could anyone confirm this is working as expected?

Thank a lot.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [June 7, 2024, 12:08pm UTC](https://discourse.julialang.org/t/behaviour-of-ref/115321/2 "2024-06-07T12:08:19Z")

</div>

Yes, this behavior is expected. Two keywords to look into are _mutation_ and _assignment_ (see for example [Assignment and mutation - #4 by StefanKarpinski](https://discourse.julialang.org/t/assignment-and-mutation/19119/4)).

I will add comments to describe:

```julia
julia> a = 5 # assign the name `a` to point to the value 5
5

julia> typeof(a)
Int64

julia> b = Ref(a) # assign the name `b` to the value `Ref(a)` which is the exact same thing as `Ref(5)`
Base.RefValue{Int64}(5)

julia> typeof(b)
Base.RefValue{Int64}

julia> b[] = 10 # mutate the contents of `b` to be 10, i.e. perform `setindex!(b, 10)`. 
10

julia> b # observe `b` with its updated contents
Base.RefValue{Int64}(10)

julia> a # the name `a` points to the same, unmutated value, 5, since that value has never been touched
5

```

Note that `Ref`s are like any other container; they contain values, and you can change their contents by mutating them.
