给定一个函数对象f,我如何找到:
在朱莉娅0.4我能找到名字使用f.env.name,但没有模块提示.对Julia 0.5来说,我找不到两个中的任何一个.
名称很简单:Symbol(f)或者string(f)如果你想要一个字符串
正如您所知,模块是每种方法(即每种类型的签名).
methods(f)返回一个方法表,根据文件打印出所有方法及其位置.
你可以[meth.module for meth in methods(f)]去那里模块
所以要使用一个例子,该collect函数.
julia> using DataStructures #so we have some non-Base definitions
julia> Symbol(collect)
:collect
julia> methods(collect)
# 5 methods for generic function "collect":
collect(r::Range) at range.jl:813
collect{T}(::Type{T}, itr) at array.jl:211
collect(itr::Base.Generator) at array.jl:265
collect{T}(q::DataStructures.Deque{T}) at /home/ubuntu/.julia/v0.5/DataStructures/src/deque.jl:170
collect(itr) at array.jl:236
julia> [meth.module for meth in methods(collect)]
5-element Array{Module,1}:
Base
Base
Base
DataStructures
Base
julia> first(methods(collect, (Deque,))).module
DataStructures
Run Code Online (Sandbox Code Playgroud)
@oxinabox 的回答是正确的。要补充的typeof(f).name.mt.name是,是 v0.5 的替代品f.env.name。这对于避免.仅应用于string非 stdlib 模块中引入的函数时发生的情况很有用。Base.function_name(f)当 Julia 版本更改时,还存在可能不太可能中断的情况。
为了获得引入函数(类型)的模块,而不是单个方法的模块,有typeof(f).name.module,或者可能更好的版本,Base.function_module(f). 方法表的模块大概是一样的;可以通过typeof(f).name.mt.module.
请注意,f.env在V0.4是直接等同typeof(f).name.mt,所以在V0.4同样f.env.name与f.env.module应用。