# How to access the variable accessible by a closure?

**URL:** <https://discourse.julialang.org/t/how-to-access-the-variable-accessible-by-a-closure/118628>\
**Category:** General Usage\
**Tags:** closure\
**Created:** [August 26, 2024, 3:16pm UTC](https://discourse.julialang.org/t/how-to-access-the-variable-accessible-by-a-closure/118628 "2024-08-26T15:16:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 26, 2024, 3:16pm UTC](https://discourse.julialang.org/t/how-to-access-the-variable-accessible-by-a-closure/118628/1 "2024-08-26T15:16:31Z")

</div>

in the following `a` is accessible by `gun()`:

```julia
function fun()
    a = 2.0
    function gun(b)
        return a + b
    end
end

g = fun()
g(3.0)

julia> g = fun()
(::var"#gun#10"{Float64}) (generic function with 1 method)

julia> g(3.0)
5.0

```

now the question is: if I have `g` given, how could I know “what” variables could it access? and “how” to access them?  
Thanks

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 26, 2024, 3:41pm UTC](https://discourse.julialang.org/t/how-to-access-the-variable-accessible-by-a-closure/118628/2 "2024-08-26T15:41:24Z")

</div>

Maybe a better way but they are just stored as fields

```julia
julia> propertynames(g)
(:a,)

```

e.g.

```julia
julia> g.a
2.0

```

I’m not sure if doing this is public API if that matters to you
