File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
source-code/cython/Classes Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ Illustration of using Cython extension types (aka cdef classes).
6
6
7
7
1 . ` points.pyx ` : implementation of a cdef class, and a Python
8
8
child class thereof.
9
+ 1 . ` points_pure.py ` : implementation in Cython's pure Python
10
+ syntax.
11
+ 1 . ` points_python.py ` : Python implementation of the class.
9
12
1 . ` driver.py ` : Python script that uses both classes.
10
13
1 . ` setup.py ` : Python installation file to build the Cython
11
14
extension.
Original file line number Diff line number Diff line change
1
+ from math import sqrt
2
+
3
+ class Point :
4
+
5
+ _x : float
6
+ _y : float
7
+
8
+ def __init__ (self , x : float , y : float ) -> None :
9
+ self .x = x
10
+ self .y = y
11
+
12
+ def distance (self , other ) -> float :
13
+ return sqrt ((self .x - other .x )* (self .x - other .x ) + (self .y - other .y )* (self .y - other .y ))
14
+
15
+ @property
16
+ def x (self ) -> float :
17
+ return self ._x
18
+
19
+ @x .setter
20
+ def x (self , value : float ) -> None :
21
+ self ._x = float (value )
22
+
23
+ @property
24
+ def y (self ) -> float :
25
+ return self ._y
26
+
27
+ @y .setter
28
+ def y (self , value : float ) -> None :
29
+ self ._y = float (value )
30
+
31
+ def __str__ (self ) -> str :
32
+ return f"Point({ self .x } , { self .y } )"
33
+
34
+
35
+ class ColoredPoint (Point ):
36
+
37
+ def __init__ (self , x , y , color ):
38
+ super ().__init__ (x , y )
39
+ self ._color = color
40
+
41
+ @property
42
+ def color (self ):
43
+ return self ._color
44
+
45
+ @color .setter
46
+ def color (self , value ):
47
+ self ._color = str (value )
48
+
49
+ def __str__ (self ):
50
+ return f"ColoredPoint({ self .x } , { self .y } , { self .color } )"
You can’t perform that action at this time.
0 commit comments