# Returning symbolic variable from modeul

**URL:** <https://discourse.julialang.org/t/returning-symbolic-variable-from-modeul/38023>\
**Category:** General Usage\
**Tags:** math, symbolic\
**Created:** [April 22, 2020, 1:08pm UTC](https://discourse.julialang.org/t/returning-symbolic-variable-from-modeul/38023 "2020-04-22T13:08:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [April 22, 2020, 1:08pm UTC](https://discourse.julialang.org/t/returning-symbolic-variable-from-modeul/38023/1 "2020-04-22T13:08:25Z")

</div>

The following code works if you run it directly in REPL

```julia
module MyM
using SymPy
export beta, return_beta
beta = symbols("β")
function return_beta()
    return -beta
end
end
MyM.return_beta()

```

But if you put `MyM` in a file `MyM.jl` and use it like this

```julia
using MyM; return_beta()

```

you will see an error

```julia
ERROR: ArgumentError: ref of NULL PyObject
Stacktrace:
 [1] getproperty(::PyCall.PyObject, ::String) at /home/xing/.julia/packages/PyCall/zqDXB/src/PyCall.jl:296
 [2] getproperty(::PyCall.PyObject, ::Symbol) at /home/xing/.julia/packages/PyCall/zqDXB/src/PyCall.jl:306
 [3] getproperty(::SymPy.Sym, ::Symbol) at /home/xing/.julia/packages/SymPy/JaxDZ/src/types.jl:119
 [4] -(::SymPy.Sym) at /home/xing/.julia/packages/SymPy/JaxDZ/src/mathops.jl:10
 [5] return_beta() at /home/xing/Dropbox/Research/2020/Bak-Sneppen/MyM.jl:6
 [6] top-level scope at REPL[1]:1

```

I also tried similar code with SymEngine.jl, exactly the same problem. Is there something wrong about my code?

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [April 22, 2020, 1:55pm UTC](https://discourse.julialang.org/t/returning-symbolic-variable-from-modeul/38023/2 "2020-04-22T13:55:05Z")

</div>

The problem with `beta` is that the underlying memory is freed when you load it from file. You need to assign `beta` in the ` __init__ ` function of your module.  
See [here](https://github.com/symengine/SymEngine.jl/blob/08df4ffe2c8901ae5b6ab3b4eb2cad9dfabb8900/src/SymEngine.jl#L35) how `SymEngine.jl` handles this.
