Export Nim types as Python classes
masterYou can export Nim types as Python classes by defining a ref object that inherits from PyNimObjectExperimental (directly or indirectly).
Requirements and Behavior:
- Inheritance: The type must be a
ref objectofPyNimObjectExperimental. - Method Exporting: A type is only exported to Python if at least one exported method is defined.
- Method vs. Global Function: A proc is exported as a Python type method only if its first argument is named
selfand is of the corresponding type. If the first argument is not namedself, the proc is exported as a global module function. - Special Methods: Specific naming patterns allow you to implement Python magic methods like
__init__and__repr__.
Warning: This feature is experimental.
# mymodule.nim
import nimpy
type TestType = ref object of PyNimObjectExperimental
myField: string
proc setMyField(self: TestType, value: string) {.exportpy.} =
self.myField = value
proc getMyField(self: TestType): string {.exportpy.} =
self.myField