Hython is a Python interpreter written in Haxe.
It's designed for:
- working on any platform you want
- working with Haxe/Haxeflixel projects
- easy integration with Haxe projects
This project is not bind to CPython, btw.
- Lightweight: Hython is designed to be lightweight, with a small memory footprint and fast execution speed, maybe at the speed of light (~4 times faster than CPython in cpp target).
- Easy Integration: Hython integrates easily with Haxe projects, making it a great choice for Haxe developers.
- Own Runtime System: Hython uses a own runtime system, to make it faster and more efficient.
see here for more information.
deffunction definitions with default arguments- Python operators:
and,or,not,in,not in,is,is not - List comprehensions:
[x * 2 for x in range(10) if x % 2 == 0] - Dictionary comprehensions:
{x: x**2 for x in range(5)} - Set comprehensions:
{x * 2 for x in range(5)} - Slicing:
my_list[1:5],my_string[::2] - Tuple support:
(1, 2, 3) - Multiple assignment:
a, b = 1, 2 - Tuple unpacking:
a, b = (1, 2) - Set literals:
{1, 2, 3} - Walrus operator:
(x := 5) globalkeyword for declaring global variables inside functionsnonlocalkeyword for declaring variables from enclosing scopesyieldkeyword for generator functions- Type hints:
def greet(name: str) -> str:,List[int],Callable[[int], str] try/except/finallyexception handlingraisestatement for throwing exceptionsmatch/casepattern matching (Python 3.10+)withstatement for context managementasync/awaitfor asynchronous programming...(ellipsis) literal- Bytes literals:
b"hello" - f-strings (basic support)
- Decorators:
@decoratorsyntax - Built-in decorators:
property(),staticmethod(),classmethod()
Hython supports the following dunder methods on classes:
__init__- Constructor__str__- String representation (str())__repr__- Representation__len__- Length (len())__add__- Addition operator (+)__sub__- Subtraction operator (-)__mul__- Multiplication operator (*)__truediv__- Division operator (/)__floordiv__- Floor division (//)__mod__- Modulo operator (%)__pow__- Power operator (**)__eq__- Equality (==)__ne__- Not equal (!=)__lt__- Less than (<)__gt__- Greater than (>)__le__- Less than or equal (<=)__ge__- Greater than or equal (>=)__contains__- Membership test (in)__getitem__- Indexing (obj[key])__call__- Callable objects
Hython includes Python-compatible built-in functions:
Type Conversion:
int(),float(),str(),bool()list(),dict(),type(),tuple()
Numeric Functions:
abs(),min(),max(),sum()round(),pow(),sqrt()hex(),oct(),bin()
Sequence Functions:
len(),range(),enumerate()sorted(),reversed(),zip()next()- get next value from iterator/generator
Logic Functions:
any(),all(),isinstance()hasattr(),getattr(),setattr(),callable()
Input/Output:
print()input()
Other:
id(),format(),vars(),ord(),chr()
mathosrandomjsondatetimere
Python module imports are hardcoding it.
[!NOTE]: It has quite a few limitations, but it's usable.
Compared to CPython, Hython currently has several limitations; Hython does not use bytecode generation, and there is no type annotation or static type checking system.
Additionally, Hython has limited support for some advanced Python features. Some Python features and behaviors may not work as expected or may not be implemented at all.
new()- Create a new interpreter instanceexecute(expr:Expr):Dynamic- Execute parsed expression treecallDef(name:String, args:Array<Dynamic>):Dynamic- Call a defined function with name and argumentshasDef(name:String):Bool- Check if a function is definedsetVar(name:String, value:Dynamic):Dynamic- Set a variablegetVar(name:String):Dynamic- Get a variabledelVar(name:String):Dynamic- Delete a variablestop(): Stop the interpreter and python code can't handle by trying or catching errors and become unusable.
maxDepth:Int- Maximum recursion depth (default: 1000)allowImport:Bool- Allow importing modules (default: true)allowClassResolve:Bool- Allow class instantiation (default: true)
new()- Create a new parser instanceparseString(code:String):Expr- Parse Python code into expression tree
Hython was originally based on NebulaStellaNova's pythonscript.
Contributions are welcome! Please read the CONTRIBUTING.md file for details on how to contribute.
This project is licensed under the MIT License - see the LICENSE.md file for details.