# How to understand Flux.functor?

**URL:** <https://discourse.julialang.org/t/how-to-understand-flux-functor/57906>\
**Category:** General Usage\
**Tags:** flux, functors\
**Created:** [March 25, 2021, 3:02am UTC](https://discourse.julialang.org/t/how-to-understand-flux-functor/57906 "2021-03-25T03:02:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pilgrimygy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pilgrimygy/32/22789_2.png) [@pilgrimygy](https://discourse.julialang.org/u/pilgrimygy)\
**Post date:** [March 25, 2021, 3:02am UTC](https://discourse.julialang.org/t/how-to-understand-flux-functor/57906/1 "2021-03-25T03:02:31Z")

</div>

Dear Community, I got a question about functor usage in Flux.jl. When I read the DQN code in ReinforcementLearningZoo, I confused the usage of the functor:

```julia
Flux.functor(x::DQNLearner) = (Q = x.approximator, Qₜ = x.target_approximator),
y -> begin
    x = @set x.approximator = y.Q
    x = @set x.target_approximator = y.Qₜ
    x
end

```

Can someone answer my doubts?

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [March 25, 2021, 4:27am UTC](https://discourse.julialang.org/t/how-to-understand-flux-functor/57906/2 "2021-03-25T04:27:14Z")

</div>

The best place to read about functors is the [Functors.jl README](https://github.com/FluxML/Functors.jl). The models in ReinforcementLearningZoo don’t use the macro, but hook into the underlying API and overload `Functors.functor` for more flexibility over how their structs are reconstructed. Also note that the `@set` macro and syntax does not come from Functors, but from [https://github.com/jw3126/Setfield.jl](https://github.com/jw3126/Setfield.jl).

---

<div class="post-metadata">

**Author:** ![pilgrimygy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pilgrimygy/32/22789_2.png) [@pilgrimygy](https://discourse.julialang.org/u/pilgrimygy)\
**Post date:** [March 25, 2021, 5:51am UTC](https://discourse.julialang.org/t/how-to-understand-flux-functor/57906/3 "2021-03-25T05:51:33Z")

</div>

Thank you very much!
