Python Libraries
Wyn can compile your code into shared libraries that Python can call directly.
Quick Start
Write a Wyn library:
wyn
// mathlib.wyn
fn add(a: int, b: int) -> int {
return a + b
}
fn factorial(n: int) -> int {
if n <= 1 { return 1 }
return n * factorial(n - 1)
}Build with --python:
sh
wyn build mathlib.wyn --pythonThis produces:
libmathlib.dylib(or.soon Linux,.dllon Windows)mathlib.py— auto-generated Python wrapper
Use from Python:
python
from mathlib import add, factorial
print(add(2, 3)) # 5
print(factorial(20)) # 2432902008176640000Type Mapping
| Wyn Type | Python Type | Notes |
|---|---|---|
int | int | 64-bit integer |
float | float | 64-bit double |
string | str | Auto encode/decode UTF-8 |
bool | bool | True/False |
Flags
| Flag | Output |
|---|---|
--shared | Shared library only |
--python | Shared library + Python wrapper |
Notes
- Functions named
mainare excluded from the wrapper - Struct methods (with
self) are excluded - String arguments are automatically encoded/decoded