blob: b03d5f7b1361b4452800841a916b84cda98500df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
cimport cython
try:
import typing
import dataclasses
except ImportError:
pass # The modules don't actually have to exists for Cython to use them as annotations
@cython.dataclasses.dataclass
cdef class MyDataclass:
# fields can be declared using annotations
a: cython.int = 0
b: double = cython.dataclasses.field(default_factory = lambda: 10, repr=False)
# fields can also be declared using `cdef`:
cdef str c
c = "hello" # assignment of default value on a separate line
# typing.InitVar and typing.ClassVar also work
d: dataclasses.InitVar[cython.double] = 5
e: typing.ClassVar[list] = []
|