为什么Julia变量在for循环范围内不存在? [英] Why do Julia variables not exist in the scope of for-loops?

查看:295
本文介绍了为什么Julia变量在for循环范围内不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Julia进行一些基本的积分估算,并使用以下代码获取UndefVarError:

I am using Julia to do some basic integration estimation and am getting an UndefVarError with the following code:

using ExcelReaders
er = ExcelReaders

etInt = 0

waveLen = er.readxl("AM0AM1_5.xls", "Spectra!A3:A2004")
eT = er.readxl("AM0AM1_5.xls", "Spectra!B3:B2004")
gTilt = er.readxl("AM0AM1_5.xls", "Spectra!C3:C2004")
dirSol = er.readxl("AM0AM1_5.xls", "Spectra!D3:D2004")  

function trArea(r::Real, l::Real, v::Array, x::Int)
    return ((1/2) * (v[x] + v[x+1]) * (r-l))
end

for x in 1:length(waveLen)-1
    etInt += trArea(waveLen[x], waveLen[x+1], eT, x)
end

错误指向第16行.据我所知,这意味着在for循环范围内未定义etInt.朱莉娅为什么会这样?

The error points to line 16. To my understanding, this means that etInt is undefined within the scope of the for-loop. Why is this the case with Julia?

推荐答案

据我了解,这意味着在for循环范围内未定义etInt.

To my understanding, this means that etInt is undefined within the scope of the for-loop.

全局变量确实存在于所有局部作用域(例如for循环)中.但是,从Julia版本1.0开始,它们在这些本地作用域中是只读的.必须对全局变量进行写访问.

Global variables do exist in all local scopes (such as a for-loop). However, from Julia version 1.0 on they are read-only in those local scopes. Write access to global variables must be made explicit.

举一个简单的例子,

julia> x = 1       
1                  

julia> for i in 1:3
           @show x # only reading a global variable
       end         
x = 1              
x = 1              
x = 1              

工作正常,

julia> for i in 1:3
           x += 1 # writing to a global variable
           @show x
       end
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at .\REPL[3]:2 [inlined]
 [2] top-level scope at .\none:0

不起作用.可以通过放置显式的global注释来治愈:

doesn't work. This can be healed by putting an explicit global annotation:

julia> for i in 1:3
           global x += 1
           @show x
       end
x = 2
x = 3
x = 4

有关更多信息,请参见 Julia文档中的范围界定"部分.

For more information, see the scoping section in the Julia documentation.

请注意,人们对此抱怨很多,并且正在 github 上进行积极讨论.和讨论(例如,此处此处).

Note that people have complained about this a lot and it's actively being discussed on github and discourse (e.g. here and here).

请注意,这些作用域规则的影响(例如,您收到的可能不直观的错误消息)实际上仅在全局范围内运行时才受到打击(例如REPL).如果将所有内容放到一个函数中-任何局部作用域-您都会得到预期的行为:

Note that the effects of these scoping rules, like the potentially unintuitive error message that you're getting, really only hit you when you operate in a global scope (like the REPL). If you put everything in a function - any local scope - you get the expected behavior:

julia> function f()
           x = 1
           for i in 1:3
               x += 1 # no global necessary
               @show x
           end
           nothing
       end
f (generic function with 1 method)

julia> f()
x = 2
x = 3
x = 4

无论如何,这是您要真正获得快速运行时间的方法,因为全局变量几乎总是不利于性能(请参见

This is anyways what you should do to really get fast runtime as global variables are almost always bad for performance (see the Performance Tips).

还请注意,所有使用IJulia的Jupyter笔记本电脑都可以按预期工作.原因是人们也想出了上下文相关的解决方案,在这种情况下, SoftGlobalScope.jl . REPL正在考虑/正在考虑类似的事情.这些是人们进行交互工作的主要场所.

Also notice that everything will work as expected in Jupyter notebooks using IJulia. The reason is that people are coming up with context dependent solutions as well, in this case SoftGlobalScope.jl. Something similar is/was being considered for the REPL. These are the main places where people work interactively.

因此,总而言之,您可以只学习作用域定义规则(这很简单),也可以等待一些讨论过的修复程序"登陆,就像jupyter笔记本电脑一样.

So, to summarize, you can either just learn the scoping rules (which is dead simple) or you wait for some of the discussed "fixes" to land as has already been the case for jupyter notebooks.

这篇关于为什么Julia变量在for循环范围内不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆