我有以下代码:
@time begin
results = nothing
for i in 1:3
if results == nothing
results = DataFrame(A=1, B=2)
else
results = vcat(results, DataFrame(A=1, B=2))
end
end
end没有@time begin/end部分,代码运行良好。
但是,在@time begin/end部分中,我得到了UndefVarError: results not defined。
有人知道怎么回事吗?
发布于 2019-03-29 15:04:25
在我看来,你的范围是错误的。results的赋值发生在块的其他部分无法访问的范围中。可以通过指定要在本地范围内进行分配来解决此问题:
@time begin
local results = nothing
for i in 1:3
if results == nothing
results = DataFrame(A=1, B=2)
else
results = vcat(results, DataFrame(A=1, B=2))
end
end
end请注意,如果不使用begin ... end块,而不使用@time,则会得到相同的错误。
https://stackoverflow.com/questions/55419579
复制相似问题