julia - 在 for everyrow 循环中使用变量会导致错误

标签 julia

我试图理解为什么在循环内使用变量 i 会导致问题

文件ma​​in2.jl:

struct MyStruct
    a::Int32
    b::Int32
    c::String
end

df = DataFrame( A=Int[], B=Int[] )
push!(df, [1, 10])
push!(df, [2, 20])
push!(df, [3, 30])

insertcols!(df, 3, :C => MyStruct.(Int32(0), Int32(0), ""))
insertcols!(df, 3, :D => Vector{MyStruct})

println(df)

i = 1
for r in eachrow(df)
    i = i + 1
end

我得到:

julia> include("main2.jl")
|     A |     B |                 D |                    C |
| Int64 | Int64 |          DataType |             MyStruct |
|-------|-------|-------------------|----------------------|
|     1 |    10 | Array{MyStruct,1} | MyStruct(0, 0, \"\") |
|     2 |    20 | Array{MyStruct,1} | MyStruct(0, 0, \"\") |
|     3 |    30 | Array{MyStruct,1} | MyStruct(0, 0, \"\") |
ERROR: LoadError: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at /usr/home/.../main2.jl:19
 [2] include at ./boot.jl:328 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1094
 [4] include(::Module, ::String) at ./Base.jl:31
 [5] include(::String) at ./client.jl:431
 [6] top-level scope at REPL[3]:1
in expression starting at /usr/home/.../main2.jl:18

julia> 

最佳答案

问题是 i 存在于全局作用域中,当您在全局作用域中编写 for 循环时,它会创建自己的子作用域。您可以通过将 for 循环更改为

来解决该问题
i = 1
for r in eachrow(df)
    global i
    i = i + 1
end

但是,在函数体内,您不需要这样做(只要 i 存在于函数体内)。

这归结为这样一个事实:在 Julia 中,您通常不应该在全局范围内编写重要的代码。将代码放入函数中被认为是正确的形式。如果代码不在函数内部,则不会对其进行编译,并且您将无法获得使用 Julia 的任何性能优势。有关范围规则的更多信息可以在这里找到:https://docs.julialang.org/en/v1/manual/variables-and-scoping/

关于这个问题以及人们更愿意发生的事情已经有很多讨论。讨论或多或少从这里开始:https://github.com/JuliaLang/julia/issues/28789然后这里提出了一个解决方案:https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/现在这里讨论了一个新的潜在解决方案:https://discourse.julialang.org/t/new-scope-solution/16707 .

目前看来,对于如何改变这种行为还没有真正的共识,因此它很可能会保持原样。就其值(value)而言,我更喜欢当前的行为,并认为它有一些优点,尽管它常常让新手感到惊讶。

关于julia - 在 for everyrow 循环中使用变量会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58001871/

相关文章:

module - 为什么多处理 Julia 会破坏我的模块导入?

python - 如何在 Python 中运行使用包的 Julia 文件?

vector - Julia:如果向量中的任何元素大于 50,则返回整个向量

julia - show() 是返回结构的 String 表示的正确方法吗?

julia - Julia 编程语言中的双点 (..) 是什么意思?

julia - Julia 中的数组/列表操作

vector - 向量中元素出现的次数 [JULIA]

visual-studio-code - 用于 Vs 代码的 Julia 自动完成的更好插件

function - Julia 中的这个递归函数是如何工作的?

julia - 在没有管道的情况下用作中缀运算符?