# How to see if a variable is defined given the variable name

**URL:** https://discourse.julialang.org/t/how-to-see-if-a-variable-is-defined-given-the-variable-name/102512
**Category:** General Usage
**Tags:** question
**Created:** [August 5, 2023, 4:35am UTC](https://discourse.julialang.org/t/how-to-see-if-a-variable-is-defined-given-the-variable-name/102512 "2023-08-05T04:35:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)
#### Post date: [August 5, 2023, 4:35am UTC](https://discourse.julialang.org/t/how-to-see-if-a-variable-is-defined-given-the-variable-name/102512/1 "2023-08-05T04:35:10Z")

</div>

Given a variable that is a string. For example:  
str = “abc”  
I would like to check if the variable corresponding to the string (in this case `abc`) is defined. I tried  
@isdefined(Symbol(str))  
but this generates an error:

ERROR: LoadError: MethodError: no method matching var"@isdefined"(::LineNumberNode, ::Module, ::Expr)

Closest candidates are:  
var"@isdefined"(::LineNumberNode, ::Module, ::Symbol)  
@ Base essentials.jl:191

What is the right way to do what I want? – Thanks.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [August 5, 2023, 5:41am UTC](https://discourse.julialang.org/t/how-to-see-if-a-variable-is-defined-given-the-variable-name/102512/2 "2023-08-05T05:41:51Z")

</div>

Being a macro, `@isdefined` is operating on the following source code as an `Expr`ession, in other words it’s seeing `:(Symbol(str))`, not `:abc`. A way to get `:abc` is to evaluate the Symbol then interpolate it into the `Expr`ession, `:($(Symbol(str)))`. Slight problem, `$`-interpolation is something done in `:()` and `quote end` runtime expressions, and few macros implement that for source code. `@isdefined` doesn’t, so you have to use a macro that does `@eval @isdefined $(Symbol(str))`. Macros are expanded at parsing of source code, so `str` must have a value by that point. As a consequence, interpolating macros like `@eval` only work in the global scope, not local scopes like function bodies that have not run and assign `str` yet. Since `@eval` only works in global scopes anyway, you could instead use the function call ~~`hasproperty(@ __MODULE__ , Symbol(str))`~~ `isdefined(@ __MODULE__ , Symbol(str))` (if you declared `global abc` without assigning it, `hasproperty` returns true but `isdefined` returns false).

Also discourse formatting tip, use backticks ` to surround a line of code and triple-backticks ``` to surround a block of code.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [August 5, 2023, 8:04am UTC](https://discourse.julialang.org/t/how-to-see-if-a-variable-is-defined-given-the-variable-name/102512/3 "2023-08-05T08:04:48Z")

</div>

Use the function instead of the macro:

```julia
str = "abc"
isdefined(Base, Symbol(str))

```

Note that you will need to also pass the module in which the symbol should be looked up.

---

<div class="post-metadata">

### Author: ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)
#### Post date: [August 5, 2023, 4:54pm UTC](https://discourse.julialang.org/t/how-to-see-if-a-variable-is-defined-given-the-variable-name/102512/4 "2023-08-05T16:54:42Z")

</div>

Thanks! This works. For me I use `isdefined(@ __MODULE__ , Symbol(str))`.
