用python创建luaapi

2024-09-22 16:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在寻找让python读取lua脚本和执行函数的方法,比如:

function foo()
*call python "test" def*
end
function bar()
print('honk')
end

在python上:

^{pr2}$

有办法做这个吗?在


Tags: 方法函数test脚本foodefbarfunction
2条回答

有一个名为lupa的包。它似乎正是你想要的(取自他们的例子):

>>> import lupa
>>> from lupa import LuaRuntime
>>> lua = LuaRuntime(unpack_returned_tuples=True)

>>> lua.eval('1+1')
2

>>> lua_func = lua.eval('function(f, n) return f(n) end')

>>> def py_add1(n): return n+1
>>> lua_func(py_add1, 2)
3

>>> lua.eval('python.eval(" 2 ** 2 ")') == 4
True
>>> lua.eval('python.builtins.str(4)') == '4'
True

你可以使用从任何一方调用的疯子库来完成。https://labix.org/lunatic-python

相关问题 更多 >