# Hey all, super confused by variables scope in Julia. I have the following function

**URL:** https://discourse.julialang.org/t/hey-all-super-confused-by-variables-scope-in-julia-i-have-the-following-function/54566
**Category:** General Usage
**Created:** [February 3, 2021, 9:38pm UTC](https://discourse.julialang.org/t/hey-all-super-confused-by-variables-scope-in-julia-i-have-the-following-function/54566 "2021-02-03T21:38:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [February 3, 2021, 9:38pm UTC](https://discourse.julialang.org/t/hey-all-super-confused-by-variables-scope-in-julia-i-have-the-following-function/54566/1 "2021-02-03T21:38:33Z")

</div>

Hey all, super confused by variables scope in Julia. I have the following function

```julia
function init_agents(;teams, workpatterns)::Vector{Agent}
    # print(typeof(teams), " ", typeof(workpatterns))
    for (teamname, info) in teams
        wp = workpatterns
        if haskey(info, "workpatterns")
            for (activitytype, activityspecs) in info["workpatterns"]
                wp[activitytype] = activityspecs
            end
        end
        print(teamname, " ", wporkpatterns, "\n")
    end
    return []
end

```

Variable `wp` is, I assume, a variable that only exists within the `for` scope, hence is reset for each iteration. However, when I update `wp[activitytype] = activityspecs`, it looks like it impacts the `workpatterns` variable itself.  
My understanding is that the `wp = workpatterns` instruction simply creates a new reference to the `workpatterns` variable, hence propagating any changes made to `wp` to `workpatterns`  
How can I make sure that _it doesn’t_? I heard about `@view` but not sure if it’s the correct solution here.

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1612388278015600?thread_ts=1612388278.015600&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [February 3, 2021, 10:57pm UTC](https://discourse.julialang.org/t/hey-all-super-confused-by-variables-scope-in-julia-i-have-the-following-function/54566/2 "2021-02-03T22:57:14Z")

</div>

Yes, `wp = workpatterns` binds the name `wp` to the same object as `workpatterns`. To make `wp` a _copy_ of `workpatterns`, write:

```julia
wp = copy(workpatterns)

```

Or if you also want to avoid modifications to `workpatterns` in case of accessing deeper levels of its structure:

```julia
wp = deepcopy(workpatterns)

```
