Integration with Python

Export to Python

When the Erg script is compiled, a .pyc file is generated, which can simply be imported as a Python module. However, variables set to private on the Erg side cannot be accessed from Python.

.public = "this is a public variable"
private = "this is a private variable"
erg --compile
import foo

print(foo.private) # AttributeError:

import from Python

By default, all objects imported from Python are of type Object. Since no comparison is possible with this type, it is necessary to narrow down the type.

Type specification in the standard library

All APIs in the Python standard library are type-specified by the Erg development team.

time = pyimport "time"
time.sleep! 1

Type specification for user scripts

Type hints on the Python side are ignored. Create a file that types the Python foo module.

X = ...
def bar(x):
def baz():
class C:
.X: Int
.bar!: Int => Int
.foo! = baz!: () => Int # aliasing
.C!: Class

No syntax other than declarations and definitions (aliasing) are allowed in

Note that all Python functions can only be registered as procedures, and all classes as variable classes.

foo = pyimport "foo"
assert!(1) in Int

This ensures type safety by performing type checking at runtime. The checking mechanism generally works as follows.

decl_proc proc!: Proc, T =
    x =>
        assert x in T.Input
        y = proc!(x)
        assert y in T.Output

This is a runtime overhead, so a project to statically type analyze Python scripts with Erg's type system is underway.