在for eachrow循环中使用变量会导致错误

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

Main2.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

似乎目前尚无关于如何更改行为的真正共识,因此可能会保持不变。对于它的价值,我更喜欢当前的行为,并认为它具有一些优点,尽管对于新手来说通常令人惊讶。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

for循环中的ZSH列表变量名会导致错误,但可以在BASH中使用

在循环中使用模数会导致分段错误(核心转储)

在循环中的内联if语句中使用break会导致语法错误

在for循环中使用隐式参数会导致挂起

在循环中使用 promise 会导致 Promise 失败

使用循环中的变量在循环中生成函数会导致变量的阴影效应

在循环中使用 setValue() 会返回“未捕获的错误”

在 for 循环中使用 fprintf 会提供错误的值

在 v-for 循环中使用 v-autocomplete 会导致无限循环

在for循环中使用变量,在next for循环中使用

在while循环中分配值会导致错误

循环中的Getpivotdata会导致运行时错误1004

列表中元素的顺序会导致for循环中的错误吗?

在 for 循环中添加 if 语句会导致“在“}”中出现“意外的 '}””错误

在fortran循环中使用指针变量

在循环中使用 getter setter 变量

Python在for循环中的列表中使用变量

在 jsx 循环中使用变量

在 postgres 循环中使用变量

如何在循环中使用变量

在for循环中使用多个变量

在for循环中使用变量的存储值

在循环中使用多个变量

为什么在循环中使用$ v0寄存器会返回错误的输出?

在for循环中使用Sklearn的LabelEncoder错误

为什么在循环条件中使用 '<=' 而不是 '<' 会导致分段错误 [SIGSEGV]?

在ngFor中使用Form HTML标记会导致ngModel使用循环变量的最新结果而不是当前变量

错误:在Haskell中使用包“ servant”导致“ ...因为类型变量't'会转义其范围”

在while循环中使用最终局部变量时出现不同的编译错误