我想检查var是数组还是Dict.
typeof(var) == Dict
typeof(var) == Array
Run Code Online (Sandbox Code Playgroud)
但它不起作用因为typeof太精确了:Dict {ASCIIString,Int64}.什么是最好的方式?
如果您需要"不太精确"的检查,您可能需要考虑使用该isa()功能,如下所示:
julia> d = Dict([("A", 1), ("B", 2)])
julia> isa(d, Dict)
true
julia> isa(d, Array)
false
julia> a = rand(1,2,3);
julia> isa(a, Dict)
false
julia> isa(a, Array)
true
Run Code Online (Sandbox Code Playgroud)
isa()然后可以在控制流构造中使用该函数,如下所示:
julia> if isa(d, Dict)
println("I'm a dictionary!")
end
I'm a dictionary!
julia> if isa(a, Array)
println("I'm an array!")
end
I'm an array!
Run Code Online (Sandbox Code Playgroud)
注意:使用Julia 0.4.3测试