Yly*_*Yly 6 julia callable-object
在Julia中,确定对象是否可调用的最佳方法是什么?(例如,是否存在python callable函数的模拟?)
编辑:这是人们所希望的:
f() = println("Hi")
x = [1,2,3]
a = 'A'
callable(f) # => true
callable(x) # => false
callable(a) # => false
callable(sin) # => true
Run Code Online (Sandbox Code Playgroud)
这个怎么样:
julia> function iscallable(f)
try
f()
return true
catch MethodError
return false
end
end
iscallable (generic function with 1 method)
julia> f() = 3
f (generic function with 1 method)
julia> iscallable(f)
true
julia> x = [1,2]
2-element Array{Int64,1}:
1
2
julia> iscallable(x)
false
Run Code Online (Sandbox Code Playgroud)
这实际上是一件相当 Pythonic 的事情(而且我怀疑效率不是很高)。有什么用例?