How to pass Mojo function to Python in Python interop?
The question is how to pass Mojo function to Python in Python interop?
For example,
# This is main.mojofrom python.python import Pythondef callback(): return 5def main(): Python.add_to_path(".") let test_module = Python.import_module("lib") print(test_module.test_interop(callback))
# This is main.mojofrom python.python import Pythondef callback(): return 5def main(): Python.add_to_path(".") let test_module = Python.import_module("lib") print(test_module.test_interop(callback))
# This is lib.pydef test_interop(func): return func()
# This is lib.pydef test_interop(func): return func()
If I run this, it will show the following message:
$ main.mojomain.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 herefrom python.python import Python^mojo: error: failed to parse the provided Mojo
$ main.mojomain.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 herefrom python.python import Python^mojo: error: failed to parse the provided Mojo