Skip to content

Commit 12296bd

Browse files
committed
New simple static typing exercise
1 parent 54900f0 commit 12296bd

File tree

8 files changed

+42
-12
lines changed

8 files changed

+42
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ courses are stored in tags.
4242
### Optimising with Cython
4343

4444
- [Creating simple extension](cython/simple-extension)
45+
- [Using static typing](cython/static-typing)
4546
- [Using C-functions](cython/c-functions)
4647
- [Optimising heat equation](cython/heat-equation)
4748

cython/simple-extension/README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Create a simple Cython module (you can name it e.g. `cyt_module.pyx`)
55
containing the following function:
66
```
77
def subtract(x, y):
8-
return x - y
8+
result = x - y
9+
return result
910
```
1011

11-
Use the provided [setup.py](setup.py) for building the extension module.
12+
Create then a **setup.py** for building the extension module.
1213
Try to utilize the module e.g. as
1314
```
1415
from cyt_module import subtract
@@ -18,10 +19,3 @@ subtract(4.5, 2)
1819
in interactive interpreter or in a simple script. Try different argument
1920
types.
2021

21-
### Type declarations
22-
23-
Declare the function arguments as **int** and rebuild the extension (Note: if working with interactive interpreter you need to exit or reload the module).
24-
What happens if you call now the subtract function with floating point
25-
arguments?
26-
27-
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
def subtract(int x, int y):
2-
return x - y
1+
def subtract(x, y):
2+
result = x - y
3+
return result

cython/static-typing/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Using static typing
2+
3+
Continue with the simple Cython module for subtracting two numbers:
4+
```
5+
def subtract(x, y):
6+
result = x - y
7+
return result
8+
```
9+
10+
Declare the function internal variable `result` as integer. Try to call the
11+
function with different types of arguments (integers and floats), what kind of
12+
results you do get?
13+
14+
Next, declare also the function arguments as integers, and rebuild the module
15+
(Note: if working with interactive interpreter you need to exit or
16+
reload the module). What happens when you now call the function with
17+
floating point arguments?
18+
19+
Finally, try to declare arguments as floating point numbers (while keeping
20+
result as integer), what happens?
21+

cython/static-typing/cyt_module.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def subtract(x, y):
2+
result = x - y
3+
return result

cython/simple-extension/setup.py renamed to cython/static-typing/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
from Cython.Build import cythonize
33

44
setup(
5-
ext_modules=cythonize("NAME-OF-MODULEFILE")
5+
ext_modules=cythonize("cyt_module.pyx")
66
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def subtract(int x, int y):
2+
cdef int result
3+
result = x - y
4+
return result
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from distutils.core import setup, Extension
2+
from Cython.Build import cythonize
3+
4+
setup(
5+
ext_modules=cythonize("cyt_module.pyx")
6+
)

0 commit comments

Comments
 (0)