Skip main navigation

Hands-on: Static typing in a simple extension

In this exercise you can practice using static type declarations in Cython modules.
© CC-BY-NC-SA 4.0 by CSC - IT Center for Science

In this exercise you can practice using static type declarations in Cython modules.

The code for this exercise is located under cython/static-typing.

Continue with the simple Cython module for subtracting two numbers:

def subtract(x, y):
result = x - y
return result

Declare the function internal variable result as integer. Try to call the
function with different types of arguments (integers and floats), what kind of
results you do get?

Next, declare also the function arguments as integers, and rebuild the module
(Note: if working with interactive interpreter you need to exit or
reload the module). What happens when you now call the function with
floating point arguments?

Finally, try to declare arguments as floating point numbers (while keeping
result as integer), what happens?

© CC-BY-NC-SA 4.0 by CSC - IT Center for Science
This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education