# How do you get all variables with a value in module's workspace?

**URL:** <https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 31, 2017, 9:56pm UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022 "2017-05-31T21:56:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 31, 2017, 9:56pm UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/1 "2017-05-31T21:56:38Z")

</div>

for example,

I have the module:

```julia
module Foo

  bar() = println("moo")
  baz() = 404

  a = 2
  b = 3.4e2
  c = a + b / baz()

end

```

how do i get a dictionary that looks like:

```julia
{
  a: 2
  b: 3.4e2
  c: 2.8415841584
}

```

// note this dictionary ignores (direct definitions of) `bar` and `baz`

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [May 31, 2017, 10:30pm UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/2 "2017-05-31T22:30:15Z")

</div>

> [@djsegal](#):
>
> module Foo
> 
> bar() = println(“moo”)  
> baz() = 404
> 
> a = 2  
> b = 3.4e2  
> c = a + b / baz()
> 
> end

```julia
julia> Dict(s=>eval(Foo, s) for s in names(Foo, true) if s != :Foo && s != :eval && !isa(eval(Foo, s), Function) && !startswith(string(s), "#"))
Dict{Symbol,Real} with 3 entries:
  :c => 2.84158
  :a => 2
  :b => 340.0

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 31, 2017, 10:37pm UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/3 "2017-05-31T22:37:41Z")

</div>

Do not use `eval` here, use `getfield` instead.

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [May 31, 2017, 10:43pm UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/4 "2017-05-31T22:43:47Z")

</div>

Ah, good point. I actually didn’t know about using `getfield` with a module.

This works too:

```julia
julia> Dict(s=>getfield(Foo, s) for s in names(Foo, true) if s != :Foo && s != :eval && !isa(getfield(Foo, s), Function) && !startswith(string(s), "#"))
Dict{Symbol,Real} with 3 entries:
  :c => 2.84158
  :a => 2
  :b => 340.0

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 31, 2017, 11:59pm UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/5 "2017-05-31T23:59:09Z")

</div>

that’s awesome!

is `s != :eval` needed, though?

it looks like `!startswith(string(s), "#"))` catches that.

were you just trying to be explicit?

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [June 1, 2017, 12:15am UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/6 "2017-06-01T00:15:32Z")

</div>

It’s probably just an artifact of the order in which I added the checks while figuring it out. I’m not surprised that one of them is redundant.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [June 1, 2017, 5:25am UTC](https://discourse.julialang.org/t/how-do-you-get-all-variables-with-a-value-in-modules-workspace/4022/7 "2017-06-01T05:25:20Z")

</div>

the code I got working with my project was:

`Dict(s=>getfield(Foo, s) for s in names(Foo, true) if s != :Foo && !startswith(string(s), "#") && !startswith(string(s), "/") && !isa(getfield(Foo, s), Function) && !isa(getfield(Foo, s), Type) && !isa(getfield(Foo, s), Module))`

beyond original question, this also ignores:

- macros
- types
- modules
- loaded files

* * *

please shout out any more suggestions you have or cleaning tips
