Skip to content

Commit 91d6982

Browse files
committed
Add pure Python implementation
1 parent 905a141 commit 91d6982

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

source-code/cython/Classes/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Illustration of using Cython extension types (aka cdef classes).
66

77
1. `points.pyx`: implementation of a cdef class, and a Python
88
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.
912
1. `driver.py`: Python script that uses both classes.
1013
1. `setup.py`: Python installation file to build the Cython
1114
extension.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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})"

0 commit comments

Comments
 (0)