Previous topic

Next topic

This Page

Quick search

As an optimizing compiler, Numba needs to decide on the type of each
variable to generate efficient machine code. Python’s standard types
are not precise enough for that, so we had to develop our own fine-grained
type system.

You will encounter Numba types mainly when trying to inspect the results
of Numba’s type inference, for debugging or
educational purposes. However, you need to use
types explicitly if compiling code ahead-of-time.

A signature specifies the type of a function. Exactly which kind
of signature is allowed depends on the context (AOT or JIT
compilation), but signatures always involve some representation of Numba
types to specifiy the concrete types for the function’s arguments and,
if required, the function’s return type.

An example function signature would be the string "f8(i4,i4)"
(or the equivalent "float64(int32,int32)") which specifies a
function taking two 32-bit integers and returning a double-precision float.

The most basic types can be expressed through simple expressions. The
symbols below refer to attributes of the main numba module (so if
you read “boolean”, it means that symbol can be accessed as numba.boolean).
Many types are available both as a canonical name and a shorthand alias,
following Numpy’s conventions.

The easy way to declare array types is to subscript an elementary type
according to the number of dimensions. For example a 1-dimension
single-precision array:

>>> numba.float32[:]array(float32, 1d, A)

or a 3-dimension array of the same underlying type:

>>> numba.float32[:,:,:]array(float32, 3d, A)

This syntax defines array types with no particular layout (producing code
that accepts both non-contiguous and contiguous arrays), but you can
specify a particular contiguity by using the ::1 index either at
the beginning or the end of the index specification:

Create an array type. dtype should be a Numba type. ndim is the
number of dimensions of the array (a positive integer). layout
is a string giving the layout of the array: A means any layout, C
means C-contiguous and F means Fortran-contiguous.