<h1 dir="auto">Version Info</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; versioninfo()
Julia Version 1.1.1
Platform Info:
  OS: Linux (x86_64-conda_cos6-linux-gnu)
  CPU: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, ivybridge)"><pre class="notranslate"><code class="notranslate">julia&gt; versioninfo()
Julia Version 1.1.1
Platform Info:
  OS: Linux (x86_64-conda_cos6-linux-gnu)
  CPU: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, ivybridge)
</code></pre></div>
<h1 dir="auto">Julia Console</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; global_vote = zeros(1)
1-element Array{Float64,1}:
 0.0
julia&gt; for i in 1:1
           global_vote = global_vote .+ 1
       end
ERROR: UndefVarError: global_vote not defined
Stacktrace:
 [1] top-level scope at ./REPL[2]:2 [inlined]
 [2] top-level scope at ./none:0"><pre class="notranslate"><code class="notranslate">julia&gt; global_vote = zeros(1)
1-element Array{Float64,1}:
 0.0
julia&gt; for i in 1:1
           global_vote = global_vote .+ 1
       end
ERROR: UndefVarError: global_vote not defined
Stacktrace:
 [1] top-level scope at ./REPL[2]:2 [inlined]
 [2] top-level scope at ./none:0
</code></pre></div>
<h1 dir="auto">Jupyter Kernel</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="global_vote = zeros(1)
for i in 1:1
    global_vote = global_vote .+ 1
end
print(global_vote)
[1.0]"><pre class="notranslate"><code class="notranslate">global_vote = zeros(1)
for i in 1:1
    global_vote = global_vote .+ 1
end
print(global_vote)
[1.0]
</code></pre></div>
<h1 dir="auto">Work Around in Julia Console</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="for i in 1:1
   global global_vote = global_vote .+ 1
end"><pre class="notranslate"><code class="notranslate">for i in 1:1
   global global_vote = global_vote .+ 1
end
</code></pre></div>
<h3 dir="auto">Example 1</h3>
<p dir="auto">This came up with a student who upgraded from 0.6 to 1.0 directly, so never even got a chance to see a deprecation warning, let alone find an explanation for new behavior:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; beforefor = true
true

julia&gt; for i in 1:2
         beforefor = false
       end

julia&gt; beforefor  # this is surprising bit
true

julia&gt; beforeif = true
true

julia&gt; if 1 == 1
         beforeif = false
       end
false

julia&gt; beforeif  # Another surprise!
false

julia&gt; function foo()
         infunc = true
         for i in 1:10
           infunc = false
         end
         @show infunc
       end
foo (generic function with 1 method)

julia&gt; foo()  # &quot;I don't get this&quot;
infunc = false "><pre class="notranslate">julia<span class="pl-k">&gt;</span> beforefor <span class="pl-k">=</span> <span class="pl-c1">true</span>
<span class="pl-c1">true</span>

julia<span class="pl-k">&gt;</span> <span class="pl-k">for</span> i <span class="pl-k">in</span> <span class="pl-c1">1</span><span class="pl-k">:</span><span class="pl-c1">2</span>
         beforefor <span class="pl-k">=</span> <span class="pl-c1">false</span>
       <span class="pl-k">end</span>

julia<span class="pl-k">&gt;</span> beforefor  <span class="pl-c"><span class="pl-c">#</span> this is surprising bit</span>
<span class="pl-c1">true</span>

julia<span class="pl-k">&gt;</span> beforeif <span class="pl-k">=</span> <span class="pl-c1">true</span>
<span class="pl-c1">true</span>

julia<span class="pl-k">&gt;</span> <span class="pl-k">if</span> <span class="pl-c1">1</span> <span class="pl-k">==</span> <span class="pl-c1">1</span>
         beforeif <span class="pl-k">=</span> <span class="pl-c1">false</span>
       <span class="pl-k">end</span>
<span class="pl-c1">false</span>

julia<span class="pl-k">&gt;</span> beforeif  <span class="pl-c"><span class="pl-c">#</span> Another surprise!</span>
<span class="pl-c1">false</span>

julia<span class="pl-k">&gt;</span> <span class="pl-k">function</span> <span class="pl-en">foo</span>()
         infunc <span class="pl-k">=</span> <span class="pl-c1">true</span>
         <span class="pl-k">for</span> i <span class="pl-k">in</span> <span class="pl-c1">1</span><span class="pl-k">:</span><span class="pl-c1">10</span>
           infunc <span class="pl-k">=</span> <span class="pl-c1">false</span>
         <span class="pl-k">end</span>
         <span class="pl-c1">@show</span> infunc
       <span class="pl-k">end</span>
foo (generic <span class="pl-k">function</span> with <span class="pl-c1">1</span> method)

julia<span class="pl-k">&gt;</span> <span class="pl-c1">foo</span>()  <span class="pl-c"><span class="pl-c">#</span> "I don't get this"</span>
infunc <span class="pl-k">=</span> <span class="pl-c1">false</span> </pre></div>
<h3 dir="auto">Example 2</h3>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; total_lines = 0
0

julia&gt; list_of_files = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]
3-element Array{String,1}:
 &quot;a&quot;
 &quot;b&quot;
 &quot;c&quot;

julia&gt; for file in list_of_files
         # fake read file
         lines_in_file = 5
         total_lines += lines_in_file
       end
ERROR: UndefVarError: total_lines not defined
Stacktrace:
 [1] top-level scope at ./REPL[3]:4 [inlined]
 [2] top-level scope at ./none:0

julia&gt; total_lines  # This crushs the students willingness to learn
0"><pre class="notranslate">julia<span class="pl-k">&gt;</span> total_lines <span class="pl-k">=</span> <span class="pl-c1">0</span>
<span class="pl-c1">0</span>

julia<span class="pl-k">&gt;</span> list_of_files <span class="pl-k">=</span> [<span class="pl-s"><span class="pl-pds">"</span>a<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>b<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>c<span class="pl-pds">"</span></span>]
<span class="pl-c1">3</span><span class="pl-k">-</span>element Array{String,<span class="pl-c1">1</span>}<span class="pl-k">:</span>
 <span class="pl-s"><span class="pl-pds">"</span>a<span class="pl-pds">"</span></span>
 <span class="pl-s"><span class="pl-pds">"</span>b<span class="pl-pds">"</span></span>
 <span class="pl-s"><span class="pl-pds">"</span>c<span class="pl-pds">"</span></span>

julia<span class="pl-k">&gt;</span> <span class="pl-k">for</span> file <span class="pl-k">in</span> list_of_files
         <span class="pl-c"><span class="pl-c">#</span> fake read file</span>
         lines_in_file <span class="pl-k">=</span> <span class="pl-c1">5</span>
         total_lines <span class="pl-k">+=</span> lines_in_file
       <span class="pl-k">end</span>
ERROR<span class="pl-k">:</span> UndefVarError<span class="pl-k">:</span> total_lines not defined
Stacktrace<span class="pl-k">:</span>
 [<span class="pl-c1">1</span>] top<span class="pl-k">-</span>level scope at <span class="pl-k">./</span>REPL[<span class="pl-c1">3</span>]<span class="pl-k">:</span><span class="pl-c1">4</span> [inlined]
 [<span class="pl-c1">2</span>] top<span class="pl-k">-</span>level scope at <span class="pl-k">./</span>none<span class="pl-k">:</span><span class="pl-c1">0</span>

julia<span class="pl-k">&gt;</span> total_lines  <span class="pl-c"><span class="pl-c">#</span> This crushs the students willingness to learn</span>
<span class="pl-c1">0</span></pre></div>
<p dir="auto">I "get" why this happens in the sense that I think I can explain, with sufficient reference to the arcana in the manual about what introduces scopes and what doesn't, but I think that this is problematic for interactive use.</p>
<p dir="auto">In example one, you get a silent failure. In example two, you get an error message that is very there-is-no-spoon. Thats roughly comparable to some Python code I wrote in a notebook at work today.</p>
<p dir="auto">I'm not sure what the rules are in Python, but I do know that generally you can't assign to things at the global scope without invoking global. But at the REPL it does work, presumably because at the REPL the rules are different or the same logic as if they were all are in the scope of function is applied.</p>
<p dir="auto">I can't language-lawyer the rules enough to propose the concrete change I would like, and based on Slack this isn't even necessarily perceived as an issue by some people, so I don't know where to go with this except to flag it.</p>
<p dir="auto">Cross-refs:<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="189236901" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/19324" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/19324/hovercard" href="https://github.com/JuliaLang/julia/pull/19324">#19324</a><br>
<a href="https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514" rel="nofollow">https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514</a><br>
<a href="https://stackoverflow.com/questions/51930537/scope-of-variables-in-julia" rel="nofollow">https://stackoverflow.com/questions/51930537/scope-of-variables-in-julia</a></p>