1

For performance reasons I would like to wrap Julia functions I am using in Python with the PyCall.pyfunctionret and PyCall.pyfunction wrappers.

One of my Julia functions returns an object that is explicitly not converted to a python object using the PyCall.pyfunctionret function. Now I would like to pass this object back into Julia and wrap the function with the PyCall.pyfunction wrapper.

The following example works, but from my understanding it does not actually benefit much from using the pyfunction wrapper, since I have to use the PyAny type and PyCall would still have to infer the type of the object. I would like to specify something more specific than PyAny in my call to pyfunction.

So I have this in julia:

function example(firstnumber, secondnumber)
    return Float64[firstnumber, secondnumber]
end

function useobject(obj::Vector{Float64})
    println("this is the object:", obj)
end

And in python:

from julia import Main
Main.eval('using PyCall; include("juliafunctions.jl")')

jl_function = Main.eval("pyfunctionret(example, Any, Float64, Float64)")

# These two work as expected - the first returns a converted python object
# (which is a numpy array in this case) and the second returns a simple
# wrapped julia object.
python_object = Main.example(1, 2)
jl_object = jl_function(1, 2)

# Now here is my problem. This works, but I would like to replace
# PyAny here, to avoid the automatic type inference pyjulia has to do in
# this case:
jl_secondfunction = Main.eval("pyfunction(useobject, PyAny)")
jl_secondfunction(jl_object)

My questions are the following:

  • Is my understanding of pyfunction and the use of the PyAny type correct, as in that PyCall still needs to infer the type of the passed in object before attempting any conversion?
  • What should I put instead of PyAny, to correctly indicate to PyCall, that I am only going to pass in a wrapped julia object which needs to be unwrapped, not converted?

I also tried Main.eval("pyfunction(useobject, PyObject)") but that just doesn't work at all and I tried Main.eval("pyfunction(useobject, Any)"), which comes slightly closer to what I want because it passes the wrapped python object into Julia. However, that fails since it doesn't find the correct method for type PyObject.

bgro
  • 65
  • 6
  • 1
    Asking this kind of question in the related repository would be more likely to be answered. – Shayan Nov 12 '22 at 08:59
  • I posted an issue on the repository here: [https://github.com/JuliaPy/PyCall.jl/issues/1013](https://github.com/JuliaPy/PyCall.jl/issues/1013). Thank you for your input @Shayan – bgro Nov 13 '22 at 11:10

0 Answers0