如何在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:
This generates anhaxe -python output.py -main Test testpy.pyoutput.pyfile 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
相关产品推荐
相关产品推荐

