我刚开始使用 Hy。
例如,我们有这个 Hy 代码:
(print "Hy, world!")
我们有两段 Python 代码。第一件:
print("Some python code")
第二张:
print("Some other python code")
如何将 Hy 代码包含到单独的文件中,然后使用 Hy 导入?
请包含 Hy 部分和 Python 部分的所有必要代码和说明(在哪里放置什么以及如何运行)。
我刚开始使用 Hy。
例如,我们有这个 Hy 代码:
(print "Hy, world!")
我们有两段 Python 代码。第一件:
print("Some python code")
第二张:
print("Some other python code")
如何将 Hy 代码包含到单独的文件中,然后使用 Hy 导入?
请包含 Hy 部分和 Python 部分的所有必要代码和说明(在哪里放置什么以及如何运行)。
不幸的是,Hy 的这方面的手册有点隐蔽(即目前不是教程的一部分)。
无论如何,你把你的Hy代码放到一个单独的文件中并命名它example.hy
(或其他):
(print "Hy, world!")
在您的 Python 脚本中,您只需hy
先导入,然后再导入example
,就像使用 Python 模块一样。
import hy
import example
这样做的原因是因为在你这样做时hy
安装了一个导入钩子import hy
,它允许它找到hy
-files,编译它们,然后像任何其他 Python 模块一样导入它们。当然,你也可以这样做:
import hy
print("Some python code")
import example
print("Some other python code")