1

例如,我们有这个 Hy 代码:

(print "Hy, world!")

我们有两段 Python 代码。第一件:

print("Some python code")

第二张:

print("Some other python code")

我们怎样才能做出这样的事情:

print("Some python code")
(print "Hy, world!")
print("Some other python code")

还请包括适当的进口。我还没有找到正确的导入方式Hy

4

2 回答 2

3

根据手册:

import hy

print("Some python code")
hy.eval(hy.read_str('(print "Hy, world!")'))
print("Some other python code")

请注意,Hy 代码是在运行时编译的。我们可能永远无法在 Python 脚本中实现嵌入 Hy 代码,以便在编译 Python 代码的同时编译它。但是,您可以执行相反的操作并将 Python 嵌入 Hy 中:

(pys "print('Some python code')")
(print "Hy, world!")
(pys "print('Some other python code')")
于 2020-05-16T18:47:56.637 回答
1

您将hy代码放入单独的文件中并命名example.hy(或其他名称):

(print "Hello World")

在您的 Python 脚本中,您只需hy先导入,然后example像使用 Python 模块一样导入。

import hy
import example

这样做的原因是因为hy在你这样做时安装了一个导入钩子import hy,它允许它找到hy-files,编译它们,然后像​​任何其他 Python 模块一样导入它们。

于 2020-05-16T18:51:30.780 回答