# PyObjects as keys

**URL:** <https://discourse.julialang.org/t/pyobjects-as-keys/26521>\
**Category:** General Usage\
**Created:** [July 18, 2019, 9:59pm UTC](https://discourse.julialang.org/t/pyobjects-as-keys/26521 "2019-07-18T21:59:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 18, 2019, 9:59pm UTC](https://discourse.julialang.org/t/pyobjects-as-keys/26521/1 "2019-07-18T21:59:29Z")

</div>

As part of some work toward [code generation from SymPy.jl](https://github.com/cscherrer/Soss.jl/blob/sym/src/symbolic.jl#L150), I need to map sympy functions to Julia functions. My current approach is a Dict:

```julia
symfuncs = Dict(
      sympy.log => log
    , sympy.Pow => :^
    , sympy.Abs => abs
)

```

Sometimes this works great, but sometimes it evaluates to

```julia
julia> Soss.symfuncs
Dict{PyCall.PyObject,Any} with 3 entries:
  PyObject NULL => :^
  PyObject NULL => abs
  PyObject NULL => log

```

Whether I get the correct result or the NULLs seems nondeterministic.

Is there a clean way to do this so it works consistently?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [July 18, 2019, 10:05pm UTC](https://discourse.julialang.org/t/pyobjects-as-keys/26521/2 "2019-07-18T22:05:16Z")

</div>

Are you defining `symfuncs` at top-level? You need to do it inside ` __init__ `. Something like

```julia
const symfuncs = Dict()

function __init__ ()
    merge!(symfuncs, Dict(
          sympy.log => log
        , sympy.Pow => :^
        , sympy.Abs => abs
    ))
end

```

See: [https://github.com/JuliaPy/PyCall.jl#using-pycall-from-julia-modules](https://github.com/JuliaPy/PyCall.jl#using-pycall-from-julia-modules)

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 18, 2019, 10:07pm UTC](https://discourse.julialang.org/t/pyobjects-as-keys/26521/3 "2019-07-18T22:07:29Z")

</div>

That looks the solution, I’ll give it a shot. Thank you!
