Skip to content

Commit 67c66d3

Browse files
committed
Add cdef class example
1 parent d9e5d02 commit 67c66d3

File tree

7 files changed

+6874
-0
lines changed

7 files changed

+6874
-0
lines changed

source-code/cython/Classes/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
VERSION = cpython-38-x86_64-linux-gnu
2+
PRIMES_LIB = primes_cython.$(VERSION).so
3+
4+
all: $(PRIMES_LIB)
5+
6+
$(PRIMES_LIB): points.pyx
7+
python setup.py build_ext --inplace
8+
9+
clean:
10+
python setup.py clean
11+
rm -f primes_cython.c $(PRIMES_LIB)

source-code/cython/Classes/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Classes
2+
3+
Illustration of using Cython extension types (aka cdef classes).
4+
5+
## What is it?
6+
7+
1. `points.pyx`: implementation of a cdef class, and a Python
8+
child class thereof.
9+
1. `driver.py`: Python script that uses both classes.
10+
1. `setup.py`: Python installation file to build the Cython
11+
extension.
12+
1. `Makefile`: make file to build the extension.

source-code/cython/Classes/driver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
3+
from points import Point, ColoredPoint
4+
5+
p = Point(1.0, -2.0)
6+
print(p)
7+
print(f'point = {p.x}, {p.y}')
8+
print(p.distance())
9+
10+
p1 = ColoredPoint(1.0, -2.0, 'blue')
11+
print(p1.color)

0 commit comments

Comments
 (0)