popeye
popeye

Reputation: 886

How to list all methods of data types or built-in/user-defined functions in julia?

I'm switching from a python background. In python there is a built-in named dir which can be called upon anything to list out every method one can perform on it.

Is there a similar way in julia to list all the methods that one can perform on a certain datatype just like python?

Upvotes: 3

Views: 470

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

Use 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.

Sample usage:

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
...

Note thay in Julia very often several methods are defined for abstract supertypes so often it is worth to use the supertypes parameter as in:

methodswith(Dict, Base, supertypes=true)

Upvotes: 4

Related Questions