You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Haxe中调用外部Python类的成员函数?

Calling Python's TestPy Class from Haxe

Let's fix your code and cover all the necessary steps to get this working smoothly:

1. Complete the trace Call

Your trace line was cut off, and since f() takes no arguments, you don't need the spread operator (...). Here's the corrected main function:

class Test {
    public static function main():Void {
        var py:TestPy = new TestPy();
        trace(py.f()); // Correct call to the no-argument method
    }
}

2. Compile to the Python Target

For Haxe to interface with your Python class, you must compile to the Python target. Here's how to set this up:

  • Save your Python class in a file (e.g., testpy.py)
  • Save your Haxe extern (TestPy.hx) and test class (Test.hx) in the same directory
  • Use the Haxe command line to compile, including your Python file in the build. Example:
    haxe -python output.py -main Test testpy.py
    
    This generates an output.py file that combines your Haxe-compiled code with the original Python class.

3. Confirm Extern Accuracy

Your extern class is already correctly defined, but double-check these details to avoid issues:

  • The class name (TestPy) matches exactly with the Python class name
  • Method names and signatures (return type, arguments) are identical between the extern and Python class. Since f() returns a string and takes no args, your extern is spot-on.

4. Run the Generated Code

Once compiled, execute the output Python file:

python output.py

You should see the trace output print "Hello World" as expected.

内容的提问来源于stack exchange,提问作者quant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:32:39