The question is how to pass Mojo function to Python in Python interop?
For example,
# This is main.mojo
from python.python import Python
def callback():
return 5
def main():
Python.add_to_path(".")
let test_module = Python.import_module("lib")
print(test_module.test_interop(callback))
# This is lib.py
def test_interop(func):
return func()
If I run this, it will show the following message:
$ main.mojo
main.mojo:9:33: error: invalid call to '__call__': argument #1 cannot be converted from 'fn() raises -> object' to 'PythonObject'
print(test_module.test_interop(callback))
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
main.mojo:1:1: note: function declared here
from python.python import Python
^
mojo: error: failed to parse the provided Mojo
mojoeven withdefis different from that it is in Python. In error message, you can see its type isfn() raises -> objectand the functions for Python in mojo is defined asPythonObject. The manual did not mention how do deal with this yet... But if you try a more simple way, it will work. Such as just callprintin a python file. What if and how to use python to execute a mojo function(which is yourcallback) is still not clear. I will check if there is a corresponding issue in their githubPythonObejctbut Mojo is unable to marshall it into Python value.