Get a list of current variables in Julia Lang

前端 未结 5 1177
情书的邮戳
情书的邮戳 2020-12-29 18:07

I am new to Julia Lang. I am coming from the background of Matlab.

In Matlab, when pressing whos command I will get all variables in the current scope;

相关标签:
5条回答
  • 2020-12-29 18:09

    Not sure if there is something better, but

    names(Main)[4:end]
    

    seems to work. The [4:end] part is because it includes :Main, :Core and :Base which I think you would not want. I hope they will always be at the beginning.

    0 讨论(0)
  • 2020-12-29 18:09

    An Update:

    whos() 
    

    ... is not working either in iJulia or at the command prompt in Julia-1.0.0.

    It is working in Julia-0.6.4, though.

    On the other hand,

    varinfo()
    

    ....prints information about the exported global variables in a module. For Example,

    julia-1.0> varinfo()
    name                    size summary                        
    –––––––––––––––– ––––––––––– –––––––––––––––––––––––––––––––
    Base                         Module                         
    Core                         Module                         
    InteractiveUtils 154.271 KiB Module                         
    Main                         Module                         
    PyPlot           781.872 KiB Module                         
    ans               50.323 KiB Plots.Plot{Plots.PyPlotBackend}
    myrepl               0 bytes typeof(myrepl)                 
    x                   88 bytes 1×6 Array{Int64,2}             
    y                    0 bytes typeof(y)                      
    

    Hope, this is found useful.

    0 讨论(0)
  • 2020-12-29 18:12

    As of version 1.1 there is also the @locals macro

    The experimental macro Base.@locals returns a dictionary of current local variable names and values

    Release notes

    0 讨论(0)
  • 2020-12-29 18:23

    You can use Julia's whos functions just like that Matlab command.

    julia> whos()
    Base                          Module
    Core                          Module
    Main                          Module
    ans                           Nothing
    
    julia> x = 5
    5
    
    julia> whos()
    Base                          Module
    Core                          Module
    Main                          Module
    ans                           Int64
    x                             Int64
    

    Any modules (packages/libraries) you import into your local scope (using using) will also show up in the list (as Modules, like Base, Core, and Main above).

    Additionally, you can ask about names exported by Modules. Base is the module containing the standard library.

    julia> whos(Base)
    !                             Function
    !=                            Function
    !==                           Function
    $                             Function
    %                             Function
    &                             Function
    *                             Function
    +                             Function
    .... (lots and lots more)
    

    Considering that that result scrolls way off my screen, you can understand why you'd want to filter the results. For that you can use Regexes. (For more info on Julia's regexes, see this manual section)

    julia> whos(r"M")
    Main                          Module
    
    julia> whos(Base, r"Match"i)
    DimensionMismatch             DataType
    RegexMatch                    DataType
    each_match                    Function
    eachmatch                     Function
    ismatch                       Function
    match                         Function
    matchall                      Function
    

    I wasn't aware of the whos function before you asked, so thanks for helping me learn something new too. :)

    Julia issue #3393 on github is about adding memory sizes to the whos output. It also references making whos return a value rather than just printing the information out.

    0 讨论(0)
  • 2020-12-29 18:23

    whos() is not available in newer versions of Julia (1.0 onward). Use varinfo() instead. For example, varinfo(Core,r".*field.*")

    0 讨论(0)
提交回复
热议问题