8

Perhaps this is something I've simply overlooked in the documentation, but how can you view a list of currently defined variables in Julia? For example, in R you can use ls() which will give you a list of user-defined objects in the current scope. Is there an equivalent in Julia?

This is very similar to this question, but it seems that the whos function (as well as names) will list modules and other things which are not user-defined. How do I simply list variables which have been defined by the user and are not exported from other modules?

Community
  • 1
  • 1
Alex A.
  • 5,466
  • 4
  • 26
  • 56
  • @musically_ut: Edited to clarify my intent. It's slightly different than that question in that I'm looking to view _only_ variables which have been defined by the user rather than exported from modules. – Alex A. Jun 22 '15 at 15:12
  • I think you're after the behavior in this pull request: https://github.com/JuliaLang/julia/pull/10108. You should add your feedback on the proposed API and help get it merged! – mbauman Jun 23 '15 at 16:18
  • 1
    @MattB.: Nice, thanks for the heads up on that. I don't have write access so I can't merge the pull request but I left a comment. The API is a little weird IMO clearly I'm a fan of the functionality. ;) – Alex A. Jun 23 '15 at 16:33
  • Right, I simply meant that your feedback would be helpful in getting it merged, since the only remaining question there seemed to be about the API. Thanks for doing so! – mbauman Jun 23 '15 at 16:39

3 Answers3

5

One possible approach is to make a variant of whos that restricts on the summary of the objects in the current module:

function whos_user(m::Module=current_module())
    for v in sort(names(m))
        s = string(v)
        if isdefined(m, v) && summary(eval(m, v)) != "Module" && s != "whos_user"
            println(s)
        end
    end
end

Then if we do

x = 1
y = "Julia"
f(n) = n + 1
whos_user()

we get

f
x
y

One could also write whos_user to return an array of symbols rather than printing:

function whos_user(m::Module=current_module())
    v = sort(names(m))
    filter(i -> isdefined(m, i) && summary(eval(m, i)) != "Module" && string(i) != "whos_user", v)
end

Then running the same test code as before, we get this:

3-element Array{Symbol,1}:
  :f
  :x
  :y

If there's no better way to do this then I'll accept this answer.

Alex A.
  • 5,466
  • 4
  • 26
  • 56
5

For Julia v0.7.0 and up, there's a nice function varinfo, which is more or less equivalent to MATLAB's whos function. varinfo still lists some module names like Base, Core, etc., but in a much more compact manner than Julia's old whos function. It also uses Markdown to render the variable table nicely, e.g.,

    julia> varinfo()
    name                    size summary
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––------
    Base                         Module
    Core                         Module
    InteractiveUtils 164.866 KiB Module
    Main                         Module
    Plots             21.028 MiB Module
    ans                144 bytes 13-element Array{Symbol,1}
    x                   48 bytes 20-element StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
    y                   48 bytes 20-element StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
    y0                 440 bytes 50-element Array{Float64,1}
    y1                 440 bytes 50-element Array{Float64,1}
    z                  3.164 KiB 20×20 Array{Float64,2}
MBT
  • 21,733
  • 19
  • 84
  • 102
BVP
  • 161
  • 1
  • 2
2

Julia has whos function, akin to MATLAB, for this task.

musically_ut
  • 34,028
  • 8
  • 94
  • 106
  • Thanks for your answer. Is there a way to make `whos` only list variables defined by the user rather than all available modules, etc.? – Alex A. Jun 22 '15 at 15:16
  • 1
    It isn't exactly what you are looking for, but assuming you are working at the REPL you can try `whos(Main)`, which will only list names in the Main module. – spencerlyon2 Jun 23 '15 at 12:32
  • @spencerlyon2: I tried that, but it still lists all of the modules and everything. For example, on JuliaBox, `whos(Main)` prints `ArrayViews Module, Base Module, ...` It is getting closer though, since user-defined variables will be in there too. – Alex A. Jun 23 '15 at 15:11
  • Yeah, I was worried you'd have too many other things in there. I don't know of a way to narrow down to only user defined objects. – spencerlyon2 Jun 23 '15 at 15:13
  • @spencerlyon2: I think I found a decent way. Posted it as an answer. – Alex A. Jun 23 '15 at 15:36