# Using Serialization to store then load a lambdified function?

**URL:** https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884
**Category:** General Usage
**Tags:** question, sympy, serialization
**Created:** [August 17, 2022, 6:20pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884 "2022-08-17T18:20:31Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [August 18, 2022, 1:05am UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/2 "2022-08-18T01:05:44Z")

</div>

The best solution could be to use [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) instead.  
The `build_function` is designed to make saving of generated functions easy. (And you could even generate `C` or `Matlab` source code…)

- First step: Creating the expression and saving it as a `.jl` file:

```julia
using Symbolics
@variables a b c
func = a^2 + b^3-c/a

# create the function expression
f_expr = build_function(func, [a,b,c])

# Note: To use the function, one needs to apply eval:
f = eval(f_expr)
y = f([1,2,3])

# to save the function, use the expression!
write("func.jl", string(f_expr))

```

- To load the function in a new session, you can simply `include` the Julia file

```julia
f = include("func.jl")
y = f([1,2,3])

```

A nice side effect is also that you don’t need any package to load the function  
and you can even inspect the definition of the generated function in the file.

---

_[View the full topic](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884)._
