Cython programs look quite similar to Python ones, mostly with added type information. Let's have a look at a simple program that computes the n
th Fibonacci number given n
:
defcompute_fibonacchi(n): """ Computes fibonacchi sequence """ a = 1 b = 1 intermediate = 0 for x in xrange(n): intermediate = a a = a + b b = intermediate return a
Let's study this program to understand what is going on under the hood when you call this function with some numeric output; let's say compute_fibonacchi(3)
.
As we know, Python is an interpreted and dynamic language, which means you do not need to declare variables before using them. This means that, at the start of a function call, the Python interpreter is agnostic about the type of value that n
will hold. When you call the function with some integral value, Python does the type inference automatically...