首页 > 解决方案 > 如何在 julia 中列出所有数据类型或内置/用户定义函数的方法?

问题描述

我正在从 python 背景切换。在 python 中,有一个内置的 nameddir可以在任何东西上调用它来列出可以对其执行的每个方法。

在julia中是否有类似的方法来列出可以在某种数据类型上执行的所有方法,就像 python 一样?

标签: julia

解决方案


使用methodswith

help?> methodswith
search: methodswith

  methodswith(typ[, module or function]; supertypes::Bool=false])

  Return an array of methods with an argument of type typ.

  The optional second argument restricts the search to a particular module or function (the default is all top-level modules).

  If keyword supertypes is true, also return arguments with a parent type of typ, excluding type Any.

示例用法:

julia> methodswith(String, Base)
[1] ==(a::String, b::String) in Base at strings/string.jl:106
[2] abspath(a::String) in Base.Filesystem at path.jl:383
[3] ascii(s::String) in Base at strings/util.jl:612
[4] chomp(s::String) in Base at strings/util.jl:125
[5] cmp(a::String, b::String) in Base at strings/string.jl:100
[6] codeunit(s::String) in Base at strings/string.jl:86
[7] codeunit(s::String, i::Integer) in Base at strings/string.jl:89
[8] dump(io::IOContext, x::String, n::Int64, indent) in Base at show.jl:1942
...

请注意,在 Julia 中,经常为抽象超类型定义了几种方法,因此通常值得使用以下supertypes参数:

methodswith(Dict, Base, supertypes=true)

推荐阅读