2.5. Supported Python features¶
Apart from the Language part below, which applies to both object mode and nopython mode, this page only lists the features supported in nopython mode.
2.5.1. Language¶
2.5.1.1. Constructs¶
Numba strives to support as much of the Python language as possible, but some language features are not available inside Numba-compiled functions:
- Function definition
- Class definition
- Exception handling (
try .. except
,try .. finally
) - Context management (the
with
statement) - Comprehensions (either list, dict, set or generator comprehensions)
- Generator delegation (
yield from
)
The raise
statement is supported in several forms:
raise
(to re-raise the current exception)raise SomeException
raise SomeException(<arguments>)
: in nopython mode, constructor arguments must be compile-time constants
Similarly, the assert
statement is supported with or without an error
message.
2.5.1.2. Function calls¶
Numba supports function calls using positional and named arguments, as well
as arguments with default values and *args
(note the argument for
*args
can only be a tuple, not a list). Explicit **kwargs
are
not supported.
2.5.1.3. Generators¶
Numba supports generator functions and is able to compile them in object mode and nopython mode. The returned generator can be used both from Numba-compiled code and from regular Python code.
Coroutine features of generators are not supported (i.e. the
generator.send()
, generator.throw()
, generator.close()
methods).
2.5.2. Built-in types¶
2.5.2.1. int, bool¶
Arithmetic operations as well as truth values are supported.
The following attributes and methods are supported:
.conjugate()
.real
.imag
2.5.2.2. float, complex¶
Arithmetic operations as well as truth values are supported.
The following attributes and methods are supported:
.conjugate()
.real
.imag
2.5.2.3. tuple¶
Tuple construction and unpacking is supported, as well as the following operations:
- comparison between tuples
- iteration and indexing over homogenous tuples
- addition (concatenation) between tuples
2.5.2.4. list¶
Creating and returning lists from JIT-compiled functions is supported,
as well as all methods and operations. Lists must be strictly homogenous:
Numba will reject any list containing objects of different types, even if
the types are compatible (for example, [1, 2.5]
is rejected as it
contains a int
and a float
).
Note
When passing a list into a JIT-compiled function, any modifications made to the list will not be visible by the Python interpreter until the function returns.
Warning
List sorting currently uses a quicksort algorithm, which has different performance characterics than the algorithm used by Python.
2.5.2.6. bytes, bytearray, memoryview¶
The bytearray
type and, on Python 3, the bytes
type
support indexing, iteration and retrieving the len().
The memoryview
type supports indexing, slicing, iteration,
retrieving the len(), and also the following attributes:
contiguous
c_contiguous
f_contiguous
itemsize
nbytes
ndim
readonly
shape
strides
2.5.3. Built-in functions¶
The following built-in functions are supported:
abs()
bool
complex
enumerate()
float
int
: only the one-argument formlen()
min()
: only the multiple-argument formmax()
: only the multiple-argument formprint()
: only numbers and strings; nofile
orsep
argumentrange
: semantics are similar to those of Python 3 even in Python 2: a range object is returned instead of an array of values.round()
sorted()
: thekey
argument is not supportedtype()
: only the one-argument form, and only on some types (e.g. numbers and named tuples)zip()
2.5.4. Standard library modules¶
2.5.4.1. array
¶
Limited support for the array.array
type is provided through
the buffer protocol. Indexing, iteration and taking the len() is supported.
All type codes are supported except for "u"
.
2.5.4.2. cmath
¶
The following functions from the cmath
module are supported:
cmath.acos()
cmath.acosh()
cmath.asin()
cmath.asinh()
cmath.atan()
cmath.atanh()
cmath.cos()
cmath.cosh()
cmath.exp()
cmath.isfinite()
cmath.isinf()
cmath.isnan()
cmath.log()
cmath.log10()
cmath.phase()
cmath.polar()
cmath.rect()
cmath.sin()
cmath.sinh()
cmath.sqrt()
cmath.tan()
cmath.tanh()
2.5.4.3. collections
¶
Named tuple classes, as returned by collections.namedtuple()
, are
supported in the same way regular tuples are supported. Attribute access
and named parameters in the constructor are also supported.
Creating a named tuple class inside Numba code is not supported; the class must be created at the global level.
2.5.4.4. ctypes
¶
Numba is able to call ctypes-declared functions with the following argument and return types:
ctypes.c_int8
ctypes.c_int16
ctypes.c_int32
ctypes.c_int64
ctypes.c_uint8
ctypes.c_uint16
ctypes.c_uint32
ctypes.c_uint64
ctypes.c_float
ctypes.c_double
ctypes.c_void_p
2.5.4.5. math
¶
The following functions from the math
module are supported:
math.acos()
math.acosh()
math.asin()
math.asinh()
math.atan()
math.atan2()
math.atanh()
math.ceil()
math.copysign()
math.cos()
math.cosh()
math.degrees()
math.erf()
math.erfc()
math.exp()
math.expm1()
math.fabs()
math.floor()
math.frexp()
math.gamma()
math.hypot()
math.isfinite()
math.isinf()
math.isnan()
math.ldexp()
math.lgamma()
math.log()
math.log10()
math.log1p()
math.pow()
math.radians()
math.sin()
math.sinh()
math.sqrt()
math.tan()
math.tanh()
math.trunc()
2.5.4.6. operator
¶
The following functions from the operator
module are supported:
operator.add()
operator.and_()
operator.div()
(Python 2 only)operator.eq()
operator.floordiv()
operator.ge()
operator.gt()
operator.iadd()
operator.iand()
operator.idiv()
(Python 2 only)operator.ifloordiv()
operator.ilshift()
operator.imatmul()
(Python 3.5 and above)operator.imod()
operator.imul()
operator.invert()
operator.ior()
operator.ipow()
operator.irshift()
operator.isub()
operator.itruediv()
operator.ixor()
operator.le()
operator.lshift()
operator.lt()
operator.matmul()
(Python 3.5 and above)operator.mod()
operator.mul()
operator.ne()
operator.neg()
operator.not_()
operator.or_()
operator.pos()
operator.pow()
operator.rshift()
operator.sub()
operator.truediv()
operator.xor()
2.5.4.7. random
¶
Numba supports top-level functions from the random
module, but does
not allow you to create individual Random instances. A Mersenne-Twister
generator is used, with a dedicated internal state. It is initialized at
startup with entropy drawn from the operating system.
random.betavariate()
random.expovariate()
random.gammavariate()
random.gauss()
random.getrandbits()
: number of bits must not be greater than 64random.lognormvariate()
random.normalvariate()
random.paretovariate()
random.randint()
random.random()
random.randrange()
random.seed()
: with an integer argument onlyrandom.shuffle()
: the sequence argument must be a one-dimension Numpy array or buffer-providing object (such as abytearray
orarray.array
); the second (optional) argument is not supportedrandom.uniform()
random.triangular()
random.vonmisesvariate()
random.weibullvariate()
Note
Calling random.seed()
from non-Numba code (or from object mode
code) will seed the Python random generator, not the Numba random generator.
Note
The generator is not thread-safe when releasing the GIL.
Also, under Unix, if creating a child process using os.fork()
or the
multiprocessing
module, the child’s random generator will inherit
the parent’s state and will therefore produce the same sequence of
numbers (except when using the “forkserver” start method under Python 3.4
and later).
See also
Numba also supports most additional distributions from the Numpy random module.
2.5.5. Third-party modules¶
2.5.5.1. cffi
¶
Similarly to ctypes, Numba is able to call into cffi-declared external functions, using the following C types:
char
short
int
long
long long
unsigned char
unsigned short
unsigned int
unsigned long
unsigned long long
int8_t
uint8_t
int16_t
uint16_t
int32_t
uint32_t
int64_t
uint64_t
float
double
char *
void *
uint8_t *
float *
double *
ssize_t
size_t
void
The from_buffer
method of cffi.FFI
and CompiledFFI
objects is
supported for passing NumPy arrays of float32
and float64
values to C
function parameters of type float *
and double *
respectively.
Out-of-line cffi modules must be registered with Numba prior to the use of any of their functions from within Numba-compiled functions:
-
numba.cffi_support.
register_module
(mod)¶ Register the cffi out-of-line module
mod
with Numba.
Inline cffi modules require no registration.