Haxe-Python Tutorial - Syntax.pythonCode()

in #programming8 years ago

Writing Python sourcecode in the Haxe sourcecode.

haxe to python

Overview

Previous part: untyped

Sometimes it is necessary to transfer some parts of the source code exactly to Python, e.g. when using Python libraries like NumPy or Pandas. To make this possible there is the function Syntax.pythonCode(). The easiest example to show this is with comments.

When compiling, all comments are usually removed. To include comments into the created Python source code they can be written inside of quotes, into the function Syntax.pythonCode(). Don't forget to include the Python comment character #.

package;
import python.Syntax;

class Main {
    
    static function main() {
        
        // This comment is removed by the compiler
        Syntax.pythonCode( "# This comment is inserted in the Python code" );
        var a = 1;
    }
    
}

Here the result after compiling:

class Main:

    @staticmethod
    def main():
        # This comment is inserted in the Python code
        a = 1

Main.main()

With more complex examples however, errors in Python could be created, for example by optimizations of the Haxe compiler. Because of this it's best to use this function only when absolutely necessary.

Examples that create errors

1. Class variables

For class variables, in Python the name of the class is added to the variable name.

package;    
import python.Syntax;

class Main {
    static var a:String;
    
    static function main() {
        a = "Hello!";
        Syntax.pythonCode( "print(a)" );
    }
}

The variable name a becomes Main.a. But inside Syntax.pythonCode() the variable is still accessed as a.

class Main:
    a = None

    @staticmethod
    def main():
        Main.a = "Hello!"
        print(a)

Main.main()

The Python interpreter reports an error

NameError: name 'a' is not defined

2. Array compiler optimization

Here an example for an optimization of the Haxe compiler:

package;
import python.Syntax;

class Main {
    
    static function main() {
        var a = [ 1, 2, 3 ];
        Syntax.pythonCode( "print( a[2] )" );
    }
    
}

The Array a is optimized away, because its values are is never accessed as array values. The compiler adds a simple underscore with numbering to the variable name.

class Main:

    @staticmethod
    def main():
        a_0 = 1
        a_1 = 2
        a_2 = 3
        print( a[2] )

Main.main()

The Python interpreter again reports the error

NameError: name 'a' is not defined

With some basic knowledge in Python however, it is not too hard to find such errors and correct them.

Calling of Syntax.pythonCode() with variables

Syntax.pythonCode() cannot be called with a variable as an argument. The text to be included has to be written directly inside of the function and inside of quotes. Something like this doesn't work:

package;
import python.Syntax;

class Main {
    
    static function main() {
        var test = "This doesn't work.";
        Syntax.pythonCode( test );
    }
}

The Python source code:

class Main:

    @staticmethod
    def main():
        test = "This doesn't work"
        python_Syntax._pythonCode(test,[])

Main.main()

The compiler includes a class python_Syntax._pythonCode. It doesn't exist in Python and creates an error.

Both untyped and Syntax.pythonCode() can be used to call external Python classes. But there is a more elegant way for that.

Next part: writing externs (coming soon)

Sort:  

Update: I made some changes to this article. It can be viewed on GitHub

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63294.77
ETH 2638.69
USDT 1.00
SBD 2.70