is there a way to list all the symbols in a module?
help?> names
names(x::Module; all::Bool = false, imported::Bool = false)
Get an array of the names exported by a Module, excluding deprecated
names. If all is true, then the list also includes non-exported names
defined in the module, deprecated names, and compiler-generated names.
If imported is true, then names explicitly imported from other
modules are also included.
As a special case, all names defined in Main are considered "exported",
since it is not idiomatic to explicitly export names from Main.
Thanks for highlighting this @fredrikekre. Ran into this couple nights ago, was having trouble with attrs
function in the Gumbo.jl
module and wanted to see the functions list (before finally finding the names
function!). Had searched and adapted other solutions, such as this from 4 yrs ago on SO:
https://stackoverflow.com/questions/28731902/any-way-to-get-a-list-of-functions-defined-in-a-module
using REPL
function functions_in(m::Module)
s = string(m)
out = REPL.REPLCompletions.completions(s * ".", length(s)+1)
return out[1]
end
functions_in (generic function with 1 method)
this provided the result:
println("List length = ", length(functions_in(Gumbo)))
Base.print_matrix(stdout, functions_in(Gumbo))
# the following one-liner probably better than Base.print_matrix
# show(stdout, "text/plain", functions_in(Gumbo))
# Output:
List length = 29
REPL.REPLCompletions.ModuleCompletion(Gumbo, "@writeandcheck")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "CGumbo")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "HTMLDocument")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "HTMLElement")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "HTMLNode")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "HTMLText")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "InvalidHTMLException")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "NullNode")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "attributes")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "attrs")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "breadthfirst")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "check_deps")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "children")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "document_from_gumbo")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "elem_tag")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "eval")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "getattr")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "gumbo_to_jl")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "gvector_to_jl")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "include")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "libgumbo")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "load_node")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "parsehtml")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "postorder")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "preorder")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "prettyprint")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "setattr!")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "tag")
REPL.REPLCompletions.ModuleCompletion(Gumbo, "text")
interesting that the basic names
function without setting all=true
or imported=true
provides:
names(Gumbo)
# Output:
17-element Array{Symbol,1}:
:Gumbo
:HTMLDocument
:HTMLElement
:HTMLNode
:HTMLText
:NullNode
:attrs
:breadthfirst
:children
:getattr
:parsehtml
:postorder
:preorder
:prettyprint
:setattr!
:tag
:text
5 more are shown with imported=true
, and those five are unique to those added using the all=true
, so the two modifiers provide independent additions to the base list. Setting both modifiers to true provides the full list of 61 for Gumbo:
show(stdout, "text/plain", names(Gumbo; all=true, imported=true))
# Output:
61-element Array{Symbol,1}:
Symbol("##parsehtml#4")
Symbol("##print#1")
Symbol("##print#2")
Symbol("##print#3")
Symbol("#@writeandcheck")
Symbol("#attributes")
Symbol("#attrs")
Symbol("#check_deps")
Symbol("#document_from_gumbo")
Symbol("#elem_tag")
Symbol("#eval")
Symbol("#getattr")
Symbol("#gumbo_to_jl")
Symbol("#gvector_to_jl")
Symbol("#include")
Symbol("#kw##parsehtml")
Symbol("#load_node")
Symbol("#parsehtml")
Symbol("#parsehtml#4")
Symbol("#prettyprint")
Symbol("#print#1")
Symbol("#print#2")
Symbol("#print#3")
Symbol("#setattr!")
Symbol("#tag")
Symbol("#text")
:(==)
Symbol("@writeandcheck")
:AbstractTrees
:CGumbo
:Gumbo
:HTMLDocument
:HTMLElement
:HTMLNode
:HTMLText
:InvalidHTMLException
:Libdl
:NullNode
:attributes
:attrs
:breadthfirst
:check_deps
:children
:document_from_gumbo
:elem_tag
:eval
:getattr
:gumbo_to_jl
:gvector_to_jl
:hash
:include
:isequal
:libgumbo
:load_node
:parsehtml
:postorder
:preorder
:prettyprint
:setattr!
:tag
:text
I donβt understand yet how similar items such as Symbol("#getattr")
and Symbol("#getattr")
work or would be called differently.
Will have to look at the source more to see how Symbol("#kw##parsehtml"
, Symbol("#parsehtml")
, and Symbol("#parsehtml#4")
are defined as well.
Thanks @Qiyamah for asking the question!
ps - it was nice to see in the Stack Overflow comments, updated info being provided such as:
You need to use names(M, true) to get unexported symbols. β Isaiah Norton Feb 26 '15 at 3:14
--
@Isaiah, great comment. I didn't know about that option. β spencerlyon2 Feb 26 '15 at 17:51
--
For Julia 1.2 @IsaiahNorton's useful comment should read names(M, all = true) β Philip Swannell Nov 19 at 13:38