PyObjects as keys

As part of some work toward code generation from SymPy.jl, I need to map sympy functions to Julia functions. My current approach is a Dict:

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

Sometimes this works great, but sometimes it evaluates to

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?

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

const symfuncs = Dict()

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


1 Like

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