从Haxe内部调用外部Python类函数

时间:2018-04-24 19:28:40

标签: python haxe

假设我在Python中有以下类:

class TestPy:
    def __init__(self):
        pass
    def f(self):
        return("Hello World")

我想从Haxe中调用函数TestPy.f。然后我可以通过

指定一个extern类来告诉编译器这个函数
extern class TestPy { 
    public function new():Void;
    public function f():String;
}

然后使用此声明来调用此函数

class Test {
    public static function main():Void {
        var py:TestPy = new TestPy();
        trace(py.f());
    }
}

这会编译,但生成的代码如下所示:

# Generated by Haxe 3.4.7
# coding: utf-8
class Text:
    __slots__ = ()
    @staticmethod
    def main():
        py = TestPy()
        print(str(py.f()))
Text.main()

无法正常工作,因为带有TestPy类的模块永远不会在代码中导入:

  

NameError:name' TestPy'未定义

所以我的问题是我如何建议Haxe在生成的代码中添加一个import语句(例如from testmodule import TestPy)?

1 个答案:

答案 0 :(得分:3)

只需向您的extern添加@:pythonImport元数据即可。 所以,像:

@:pythonImport('testmodule', 'TestPy')
extern class TestPy {...

免责声明:尚未对此进行测试,因此可能不是完全正确的答案,但元数据为documented in the manual

相关问题