|
| 1 | +## Exporting Nim types as Python classes |
| 2 | + |
| 3 | +Warning! This is experimental. |
| 4 | +* An exported type should be a ref object and inherits `PyNimObjectExperimental` directly or indirectly. |
| 5 | +* The type will only be exported if at least one exported "method" is defined. |
| 6 | +* A proc will be exported as python type method *only* if it's first argument is of the corresponding type and is called `self`. If the first argument is not called `self`, the proc will exported as a global module function. |
| 7 | +* If you define functions that looks like initTestType or `$`, they will be exported as `__init__` and `__repr__` if the requirements are met. |
| 8 | + |
| 9 | +#### Simple Example |
| 10 | +```nim |
| 11 | +# mymodule.nim |
| 12 | +type TestType = ref object of PyNimObjectExperimental |
| 13 | + myField: string |
| 14 | +
|
| 15 | +proc setMyField(self: TestType, value: string) {.exportpy.} = |
| 16 | + self.myField = value |
| 17 | +
|
| 18 | +proc getMyField(self: TestType): string {.exportpy.} = |
| 19 | + self.myField |
| 20 | +``` |
| 21 | + |
| 22 | +``` py |
| 23 | +# test.py |
| 24 | +import mymodule |
| 25 | +tt = mymodule.TestType() |
| 26 | +tt.setMyField("Hello") |
| 27 | +assert(tt.getMyField() == "Hello") |
| 28 | +``` |
| 29 | + |
| 30 | +#### `__init__`, and `__repr__` |
| 31 | +* [example](../tests/export_pytype.nim) |
| 32 | +```nim |
| 33 | +# simple.nim |
| 34 | +## compile as simple.so |
| 35 | +
|
| 36 | +import nimpy |
| 37 | +import strformat |
| 38 | +
|
| 39 | +pyExportModule("simple") # only needed if your filename is not simple.nim |
| 40 | +
|
| 41 | +type |
| 42 | + SimpleObj* = ref object of PyNimObjectExperimental |
| 43 | + a* : int |
| 44 | +
|
| 45 | +## if |
| 46 | +## 1) the function name is like `init##TypeName` |
| 47 | +## 2) there is at least one argument |
| 48 | +## 3) the first argument name is "self" |
| 49 | +## 4) the first argument type is `##TypeName` |
| 50 | +## 5) there is no return type |
| 51 | +## we export this function as a python object method __init__ (tp_init in PyTypeObject) |
| 52 | +proc initSimpleObj*(self : SimpleObj, a : int = 1) {.exportpy} = |
| 53 | + echo "Calling initSimpleObj for SimpleObj" |
| 54 | + self.a = a |
| 55 | +
|
| 56 | +## if |
| 57 | +## 1) the function name is like `$` |
| 58 | +## 2) there is only one argument |
| 59 | +## 3) the first argument name is "self" |
| 60 | +## 4) the first argument type is `##TypeName` |
| 61 | +## 5) the return type is `string` |
| 62 | +## we export this function as a python object method __repr__ (tp_repr in PyTypeObject) |
| 63 | +proc `$`*(self : SimpleObj): string {.exportpy.} = |
| 64 | + &"SimpleObj : a={self.a}" |
| 65 | +
|
| 66 | +
|
| 67 | +## Change doc string |
| 68 | +setModuleDocString("This is a test module") |
| 69 | +setDocStringForType(SimpleObj, "This is a test type") |
| 70 | +``` |
| 71 | + |
| 72 | +* Compile as `simple.so` |
| 73 | +```bash |
| 74 | +nim c --app:lib -o:./simple.so ./simple.nim |
| 75 | +``` |
| 76 | + |
| 77 | +* Use the exported python type in python |
| 78 | +```python |
| 79 | +import simple |
| 80 | +print(simple.__doc__) |
| 81 | +print(simple.SimpleObj.__doc__) |
| 82 | +obj = simple.SimpleObj(a = 2) |
| 83 | +print(obj) |
| 84 | +``` |
0 commit comments