获取Julia Lang中当前变量的列表

获取Julia Lang中当前变量的列表,julia,Julia,我是Julia Lang的新手。我来自Matlab的背景 在Matlab中,当按下whos命令时,我将得到当前范围内的所有变量;而且,我可以将它们存储在另一个变量中,比如x=whosJulia中是否存在此类命令? Matlab中的示例代码: >> a=3; >> b=4; >> whos Variables in the current scope: Attr Name Size Bytes Class

我是Julia Lang的新手。我来自Matlab的背景

在Matlab中,当按下
whos
命令时,我将得到当前范围内的所有变量;而且,我可以将它们存储在另一个变量中,比如
x=whosJulia中是否存在此类命令?
Matlab中的示例代码:

>> a=3;
>> b=4;
>> whos
Variables in the current scope:

Attr Name        Size                     Bytes  Class
==== ====        ====                     =====  ===== 
    a            1x1                          8  double
    b            1x1                          8  double
    prefix       1x16                        16  char

Total is 18 elements using 32 bytes.

不确定是否有更好的,但是

names(Main)[4:end]

似乎有效。
[4:end]
部分是因为它包括
:Main
:Core
:Base
,我想这是您不想要的。我希望他们总是在开始的时候

您可以像Matlab命令一样使用Julia的
whos
函数

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
导入本地范围的任何模块(包/库)(使用
使用
)也将显示在列表中(如上面的Base、Core和Main等
模块)

此外,您还可以询问模块导出的名称
Base
是包含标准库的模块

julia> whos(Base)
!                             Function
!=                            Function
!==                           Function
$                             Function
%                             Function
&                             Function
*                             Function
+                             Function
.... (lots and lots more)
考虑到该结果会从我的屏幕上滚走,您可以理解为什么要过滤结果。为此,可以使用正则表达式。(有关Julia正则表达式的更多信息,请参阅)

在您询问之前,我不知道
whos
函数,因此也感谢您帮助我学习一些新知识。:)

在github上,向
whos
输出添加内存大小。它还引用了使
whos
返回一个值,而不仅仅是打印信息。

更新:

whos() 
。。。在iJulia中或在Julia-1.0.0中的命令提示符下都不起作用

不过,它在Julia-0.6.4中工作

另一方面,

varinfo()
..打印有关模块中导出的全局变量的信息。比如说,

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)                      

希望,这是有用的。

whos()
在较新版本的Julia(1.0以后)中不可用。改用
varinfo()
。例如,
varinfo(Core,r.*field.*)

从1.1版开始,还有@locals宏

实验性宏基。@locals返回当前局部变量名称和值的字典


嗯,我明白了!我想这是朱莉娅工作室的问题。谁在命令提示符下工作。你考虑过用IJulia笔记本代替Julia Studio吗?这不再有效了。请看下面。这应该是Jupyterlab的新答案,它甚至可以生成一个格式很好的表格——谢谢!请注意,正如报价中提到的,这只包括局部变量。
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)