如何检查Julia中的模块内是否定义了变量?

Dav*_*rks 5 julia

isdefined(:x) 将告诉您是否在当前工作空间中定义了变量x.

如果我想检查模块中定义的变量(不是导出的变量),我该怎么做?我尝试了以下所有方法:

julia> module Test
       x = 1
       end
Test

julia> x
ERROR: UndefVarError: x not defined

julia> isdefined(:x)
false

julia> Test.x
1

julia> isdefined(:Test.x)
ERROR: type Symbol has no field x

julia> isdefined(:Test.:x)
ERROR: TypeError: getfield: expected Symbol, got QuoteNode

julia> isdefined(Test.:x)
ERROR: TypeError: getfield: expected Symbol, got QuoteNode
Run Code Online (Sandbox Code Playgroud)

在上面的模块测试中,我想检查是否定义了x.

Dan*_*etz 11

isdefined有一个可选参数来执行此操作.尝试:

isdefined(Test, :x)
Run Code Online (Sandbox Code Playgroud)

通过常规渠道提供更多信息:?isdefined在REPL和书中:http://docs.julialang.org/en/release-0.4/stdlib/base/#Base.isdefined(链接可能是旧版本,所以目前占主导地位的搜索引擎将有所帮


Dav*_*ers 5

我想你需要

:x in names(Test) 
Run Code Online (Sandbox Code Playgroud)

  • 或者`isdefined(Test,:x)`.`isdefined`有一个optinal参数. (7认同)