julia - 如何在 julia 中列出所有数据类型或内置/用户定义函数的方法?
问题描述
我正在从 python 背景切换。在 python 中,有一个内置的 nameddir
可以在任何东西上调用它来列出可以对其执行的每个方法。
在julia中是否有类似的方法来列出可以在某种数据类型上执行的所有方法,就像 python 一样?
解决方案
使用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)
推荐阅读
- eclipse - Eclipse 仅将 WEB-INF/web.xml 从 Spring Boot 应用程序部署到 Tomcat
- reactjs - 在哪里设置子组件中来自父组件的道具值?
- networking - 在 Packet Tracer 中设置网络
- odoo-9 - 我希望从我的 Controller 类中获取用户通知的数量
- ruby-on-rails - 在rails中使用searchkick时我们需要安装elastic-search吗
- javascript - 如何用来自另一个对象的匹配键值替换对象键
- java - 在android oreo和pie上无法以打盹模式启动Activity
- python - 将列表转换为矩阵
- pandas - 在 pandas 数据框中重复行,列值顺序更改
- python - 在 3 个进程之间写入(+锁定)/读取文件的 Python 脚本