# 2.6。支持的 Python 功能
> 原文: [http://numba.pydata.org/numba-doc/latest/reference/pysupported.html](http://numba.pydata.org/numba-doc/latest/reference/pysupported.html)
除了下面的[語言](#pysupported-language)部分,它適用于[對象模式](../glossary.html#term-object-mode)和 [nopython 模式](../glossary.html#term-nopython-mode),此頁面僅列出 [nopython 模式](../glossary.html#term-nopython-mode)支持的功能]。
警告
在某些情況下,Numba 行為與 Python 語義不同。我們強烈建議審查[與 Python 語義](pysemantics.html#pysemantics)的偏差,以熟悉這些差異。
## 2.6.1。語言
### 2.6.1.1。構造
Numba 努力支持盡可能多的 Python 語言,但 Numba 編譯的函數中沒有一些語言功能。目前不支持以下 Python 語言功能:
* 類定義
* 異常處理(`try .. except`,`try .. finally`)
* 上下文管理(`with`語句)
* 一些理解(支持列表理解,但不支持字典,集合或生成器理解)
* 發電機代表團(`yield from`)
`raise`語句以多種形式支持:
* `raise`(重新提高當前例外)
* `raise SomeException`
* `raise SomeException(<arguments>)`:在 [nopython 模式](../glossary.html#term-nopython-mode)中,構造函數參數必須是[編譯時常量](../glossary.html#term-compile-time-constant)
同樣,支持`assert`語句,有或沒有錯誤消息。
### 2.6.1.2。功能
#### 2.6.1.2.1。函數調用
Numba 支持使用位置和命名參數的函數調用,以及具有默認值和`*args`的參數(注意`*args`的參數只能是元組,而不是列表)。不支持顯式`**kwargs`。
只要可以完全內聯函數,就支持對本地定義的內部函數的函數調用。
#### 2.6.1.2.2。作為參數的函數
函數可以作為參數傳遞給另一個函數。但是,他們無法歸還。例如:
```py
from numba import jit
@jit
def add1(x):
return x + 1
@jit
def bar(fn, x):
return fn(x)
@jit
def foo(x):
return bar(add1, x)
# Passing add1 within numba compiled code.
print(foo(1))
# Passing add1 into bar from interpreted code
print(bar(add1, 1))
```
注意
Numba 不將函數對象作為真實對象處理。將函數分配給變量后,無法將變量重新分配給其他函數。
#### 2.6.1.2.3。內部功能和閉合
Numba 現在支持內部函數,只要它們是非遞歸的并且只在本地調用,但不作為參數傳遞或作為結果返回。還支持在內部函數內使用閉包變量(在外部作用域中定義的變量)。
#### 2.6.1.2.4。遞歸調用
支持大多數遞歸調用模式。唯一的限制是遞歸被調用者必須有一個返回的控制流路徑而不會遞歸。 Numba 能夠在不指定函數類型簽名的情況下對類型推斷遞歸函數(這在 numba 0.28 及更早版本中是必需的)。遞歸調用甚至可以調用函數的不同重載。
### 2.6.1.3。發電機
Numba 支持生成器功能,并且能夠在[對象模式](../glossary.html#term-object-mode)和 [nopython 模式](../glossary.html#term-nopython-mode)中編譯它們。返回的生成器既可以使用 Numba 編譯的代碼,也可以使用常規的 Python 代碼。
不支持發生器的協同特征(即 [`generator.send()`](https://docs.python.org/3/reference/expressions.html#generator.send "(in Python v3.7)") , [`generator.throw()`](https://docs.python.org/3/reference/expressions.html#generator.throw "(in Python v3.7)") , [`generator.close()`](https://docs.python.org/3/reference/expressions.html#generator.close "(in Python v3.7)") 方法)。
## 2.6.2。內置類型
### 2.6.2.1。 int,bool
支持算術運算和真值。
支持以下屬性和方法:
* `.conjugate()`
* `.real`
* `.imag`
### 2.6.2.2。浮動,復雜
支持算術運算和真值。
支持以下屬性和方法:
* `.conjugate()`
* `.real`
* `.imag`
### 2.6.2.3。 str
Numba 在 Python 3 中支持(Unicode)字符串。字符串可以作為參數傳遞到 [nopython 模式](../glossary.html#term-nopython-mode),以及從 [nopython 模式](../glossary.html#term-nopython-mode)構造和返回。與 Python 一樣,切片(即使長度為 1)也會返回一個新的引用計數字符串。將來可能會引入有效訪問單個字符的優化代碼路徑。
內存中的表示與 Python 3.4 中引入的相同,每個字符串都有一個標記,用于指示字符串是否在內存中使用 1,2 或 4 字節字符寬度。當組合不同編碼的字符串時(如串聯),結果字符串自動使用兩個輸入字符串的較大字符寬度。字符串切片也使用與原始字符串相同的字符寬度,即使切片可以用較窄的字符寬度表示。 (當然,這些細節對用戶是不可見的。)
目前支持以下函數,屬性和方法:
* `len()`
* `+`(字符串串聯)
* `in`,`.contains()`
* `==`,`<`,`<=`,`>`,`>=`(比較)
* `.startswith()`
* `.endswith()`
* `.find()`
* `.split()`
* `.join()`
在未來的 Numba 版本中將添加其他操作以及對 Python 2 字符串/ Python 3 字節的支持。可能永遠不會支持 Python 2 Unicode 對象。
警告
已知某些操作的性能比 CPython 實現慢。這些包括子串搜索(`in`,`.contains()`和`find()`)和字符串創建(如`.split()`)。提高字符串性能是一項持續的任務,但 CPython 的速度不可能單獨超越基本的字符串操作。 Numba 最成功地用于碰巧涉及字符串的較大算法,其中基本字符串操作不是瓶頸。
### 2.6.2.4。元組
支持以下操作:
* 元組結構
* 元組拆包
* 元組之間的比較
* 對同類元組進行迭代和索引
* 元組之間的加法(連接)
* 用常量切片切片元組
* 元組的索引方法
### 2.6.2.5。清單
支持從 JIT 編譯的函數創建和返回列表,以及所有方法和操作。列表必須嚴格同質:Numba 將拒絕任何包含不同類型對象的列表,即使類型是兼容的(例如,`[1, 2.5]`被拒絕,因為它包含 [`int`](https://docs.python.org/3/library/functions.html#int "(in Python v3.7)") 和 [`float`](https://docs.python.org/3/library/functions.html#float "(in Python v3.7)") )。
例如,要創建數組列表:
```py
In [1]: from numba import njit
In [2]: import numpy as np
In [3]: @njit
...: def foo(x):
...: lst = []
...: for i in range(x):
...: lst.append(np.arange(i))
...: return lst
...:
In [4]: foo(4)
Out[4]: [array([], dtype=int64), array([0]), array([0, 1]), array([0, 1, 2])]
```
#### 2.6.2.5.1。列表反射
在 nopython 模式下,Numba 不對 Python 對象進行操作。 `list`被編譯成內部表示。任何`list`參數必須在進入 nopython 模式的過程中轉換為此表示形式,并且必須通過稱為 [reflection](../glossary.html#term-reflection) 的進程在原始 Python 對象中恢復其包含的元素。需要反射來維護與常規 Python 代碼中相同的語義。但是,對于大型列表,反射過程可能很昂貴,而對于包含反射數據類型的列表則不支持。由于此限制,用戶無法使用 list-of-list 作為參數。
注意
將列表傳遞給 JIT 編譯的函數時,在函數返回之前,對 Python 解釋器不會顯示對列表所做的任何修改。 (反射過程的局限性。)
警告
列表排序當前使用快速排序算法,該算法具有與 Python 使用的算法不同的性能特征。
#### 2.6.2.5.2。列表理解
Numba 支持列表理解。例如:
```py
In [1]: from numba import njit
In [2]: @njit
...: def foo(x):
...: return [[i for i in range(n)] for n in range(x)]
...:
In [3]: foo(3)
Out[3]: [[], [0], [0, 1]]
```
注意
在版本 0.39.0 之前,Numba 不支持創建嵌套列表。
Numba 還支持“數組理解”,這是一個列表理解,緊接著是對 [`numpy.array()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array "(in NumPy v1.16)") 的調用。以下是生成 2D Numpy 數組的示例:
```py
from numba import jit
import numpy as np
@jit(nopython=True)
def f(n):
return np.array([ [ x * y for x in range(n) ] for y in range(n) ])
```
在這種情況下,Numba 能夠優化程序以直接分配和初始化結果數組,而無需分配中間列表對象。因此,這里列表推導的嵌套不是問題,因為這里創建了多維數組而不是嵌套列表。
此外,當與 CPU 上的[并行](../user/jit.html#parallel-jit-option)選項結合使用時,Numba 支持并行陣列壓縮。
### 2.6.2.6。設置
JIT 編譯的函數支持集合上的所有方法和操作。
集必須是嚴格同質的:Numba 將拒絕包含不同類型對象的任何集合,即使類型是兼容的(例如,`{1, 2.5}`被拒絕,因為它包含 [`int`](https://docs.python.org/3/library/functions.html#int "(in Python v3.7)") 和 [`float`](https://docs.python.org/3/library/functions.html#float "(in Python v3.7)") )。
注意
將一個集合傳遞給 JIT 編譯的函數時,在該函數返回之前,對該集合所做的任何修改都不會對 Python 解釋器可見。
### 2.6.2.7。字典
警告
`numba.typed.Dict`是一項實驗性功能。 API 可能會在將來的版本中發生變化。
Numba 不直接支持 Python `dict`,因為它是一個無類型的容器,可以將任何 Python 類型作為成員。為了生成有效的機器代碼,Numba 需要密鑰和字典的值具有預先聲明的固定類型。為了實現這一點,Numba 有一個類型字典`numba.typed.Dict`,用戶必須使用`Dict.empty()`構造函數方法顯式聲明 key-type 和 value-type。這個類型化的字典與 Python `dict`具有相同的 API,它實現了`collections.MutableMapping`接口,可用于解釋的 Python 代碼和 JIT 編譯的 Numba 函數。因為鍵入的字典在 Numba 的本機,未裝箱的數據布局中存儲鍵和值,所以將 Numba 字典傳遞到 nopython 模式的開銷非常低。但是,這意味著使用 Python 解釋器中的類型字典比常規字典慢,因為 Numba 在獲取或設置項目時必須對 key 和 unbox 進行包裝和取消設置。
與 Python 的`dict`相比,類型字典的一個重要區別是當存儲鍵或值時會發生**隱式轉換**。因此,如果類型轉換失敗, _setitem_ 操作可能會失敗。
注意
`numba.typed.Dict`尚未使用`Dict()`構造,必須使用`Dict.empty(key_type, value_type)`類方法來構造類型化字典。
應該注意,Numba 類型字典是使用與 CPython 3.7 字典相同的算法實現的。因此,鍵入的字典是有序的,并且具有與 CPython 實現相同的沖突解決方案。
除了上面關于類型規范之外,對于可以用作鍵入字典中的鍵和/或值的類型存在限制,最值得注意的是當前不支持 Numba `Set`和`List`類型。可接受的鍵/值類型包括但不限于:unicode 字符串,數組,標量,元組。隨著 Numba 不斷改進,預計這些限制將會放松。
這是從解釋代碼創建`numba.typed.Dict`實例并在 jit 代碼中使用字典的示例:
from `ex_typed_dict_from_cpython` of `examples/dict_usage.py`
|
```py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
```
|
```py
import numpy as np
from numba import njit
from numba import types
from numba.typed import Dict
# The Dict.empty() constructs a typed dictionary.
# The key and value typed must be explicitly declared.
d = Dict.empty(
key_type=types.unicode_type,
value_type=types.float64[:],
)
# The typed-dict can be used from the interpreter.
d['posx'] = np.asarray([1, 0.5, 2], dtype='f8')
d['posy'] = np.asarray([1.5, 3.5, 2], dtype='f8')
d['velx'] = np.asarray([0.5, 0, 0.7], dtype='f8')
d['vely'] = np.asarray([0.2, -0.2, 0.1], dtype='f8')
# Here's a function that expects a typed-dict as the argument
@njit
def move(d):
# inplace operations on the arrays
d['posx'] += d['velx']
d['posy'] += d['vely']
print('posx: ', d['posx']) # Out: posx: [1\. 0.5 2\. ]
print('posy: ', d['posy']) # Out: posy: [1.5 3.5 2\. ]
# Call move(d) to inplace update the arrays in the typed-dict.
move(d)
print('posx: ', d['posx']) # Out: posx: [1.5 0.5 2.7]
print('posy: ', d['posy']) # Out: posy: [1.7 3.3 2.1]
```
|
這是一個從 jit 代碼創建`numba.typed.Dict`實例并在解釋代碼中使用字典的示例:
from `ex_typed_dict_njit` of `examples/dict_usage.py`
|
```py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
```
|
```py
import numpy as np
from numba import njit
from numba import types
from numba.typed import Dict
# Make array type. Type-expression is not supported in jit functions.
float_array = types.float64[:]
@njit
def foo():
# Make dictionary
d = Dict.empty(
key_type=types.unicode_type,
value_type=float_array,
)
# Fill the dictionary
d["posx"] = np.arange(3).astype(np.float64)
d["posy"] = np.arange(3, 6).astype(np.float64)
return d
d = foo()
# Print the dictionary
print(d) # Out: {posx: [0\. 1\. 2.], posy: [3\. 4\. 5.]}
```
|
### 2.6.2.8。無
身份測試支持 None 值(使用 [`optional`](types.html#numba.optional "numba.optional") 類型時)。
### 2.6.2.9。 bytes,bytearray,memoryview
[`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray "(in Python v3.7)") 類型,在 Python 3 上, [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes "(in Python v3.7)") 類型支持索引,迭代和檢索 len()。
[`memoryview`](https://docs.python.org/3/library/stdtypes.html#memoryview "(in Python v3.7)") 類型支持索引,切片,迭代,檢索 len()以及以下屬性:
* [`contiguous`](https://docs.python.org/3/library/stdtypes.html#memoryview.contiguous "(in Python v3.7)")
* [`c_contiguous`](https://docs.python.org/3/library/stdtypes.html#memoryview.c_contiguous "(in Python v3.7)")
* [`f_contiguous`](https://docs.python.org/3/library/stdtypes.html#memoryview.f_contiguous "(in Python v3.7)")
* [`itemsize`](https://docs.python.org/3/library/stdtypes.html#memoryview.itemsize "(in Python v3.7)")
* [`nbytes`](https://docs.python.org/3/library/stdtypes.html#memoryview.nbytes "(in Python v3.7)")
* [`ndim`](https://docs.python.org/3/library/stdtypes.html#memoryview.ndim "(in Python v3.7)")
* [`readonly`](https://docs.python.org/3/library/stdtypes.html#memoryview.readonly "(in Python v3.7)")
* [`shape`](https://docs.python.org/3/library/stdtypes.html#memoryview.shape "(in Python v3.7)")
* [`strides`](https://docs.python.org/3/library/stdtypes.html#memoryview.strides "(in Python v3.7)")
## 2.6.3。內置功能
支持以下內置函數:
* [`abs()`](https://docs.python.org/3/library/functions.html#abs "(in Python v3.7)")
* [`bool`](https://docs.python.org/3/library/functions.html#bool "(in Python v3.7)")
* [`complex`](https://docs.python.org/3/library/functions.html#complex "(in Python v3.7)")
* [`divmod()`](https://docs.python.org/3/library/functions.html#divmod "(in Python v3.7)")
* [`enumerate()`](https://docs.python.org/3/library/functions.html#enumerate "(in Python v3.7)")
* [`float`](https://docs.python.org/3/library/functions.html#float "(in Python v3.7)")
* [`hash()`](https://docs.python.org/3/library/functions.html#hash "(in Python v3.7)") (參見下面的 [Hashing](#pysupported-hashing) )
* [`int`](https://docs.python.org/3/library/functions.html#int "(in Python v3.7)") :只有單參數形式
* [`iter()`](https://docs.python.org/3/library/functions.html#iter "(in Python v3.7)") :只有單參數形式
* [`len()`](https://docs.python.org/3/library/functions.html#len "(in Python v3.7)")
* [`min()`](https://docs.python.org/3/library/functions.html#min "(in Python v3.7)")
* [`max()`](https://docs.python.org/3/library/functions.html#max "(in Python v3.7)")
* [`next()`](https://docs.python.org/3/library/functions.html#next "(in Python v3.7)") :只有單參數形式
* [`print()`](https://docs.python.org/3/library/functions.html#print "(in Python v3.7)") :僅限數字和字符串;沒有`file`或`sep`參數
* [`range`](https://docs.python.org/3/library/stdtypes.html#range "(in Python v3.7)") :即使在 Python 2 中,語義也類似于 Python 3:返回范圍對象而不是值數組。范圍的唯一允許使用是作為可調用函數(不能將范圍作為參數傳遞給 jitted 函數或從 jitted 函數返回范圍)。
* [`round()`](https://docs.python.org/3/library/functions.html#round "(in Python v3.7)")
* [`sorted()`](https://docs.python.org/3/library/functions.html#sorted "(in Python v3.7)") :不支持`key`參數
* `type()`:只有單參數形式,并且只在某些類型上(例如數字和命名元組)
* [`zip()`](https://docs.python.org/3/library/functions.html#zip "(in Python v3.7)")
### 2.6.3.1。哈希
支持 [`hash()`](https://docs.python.org/3/library/functions.html#hash "(in Python v3.7)") 內置并為所有支持的可哈希類型生成哈希值,具有以下特定于 Python 版本的行為:
在 Python 3 下,Numba 計算的哈希值將與`sys.hash_info.algorithm`為`siphash24`(默認值)的條件下 CPython 中計算的哈希值完全匹配。
在 Python 2 下,由 Numba 計算的哈希值將遵循為 Python 3 描述的行為,`sys.hash_info.algorithm`被模擬為`siphash24`。沒有嘗試復制 Python 2 散列行為。
`PYTHONHASHSEED`環境變量以與 CPython 文檔中描述的方式完全相同的方式影響散列行為。
## 2.6.4。標準庫模塊
### 2.6.4.1。 `array`
通過緩沖協議提供對 [`array.array`](https://docs.python.org/3/library/array.html#array.array "(in Python v3.7)") 類型的有限支持。支持索引,迭代和獲取 len()。除`"u"`外,支持所有類型代碼。
### 2.6.4.2。 `cmath`
支持 [`cmath`](https://docs.python.org/3/library/cmath.html#module-cmath "(in Python v3.7)") 模塊的以下功能:
* [`cmath.acos()`](https://docs.python.org/3/library/cmath.html#cmath.acos "(in Python v3.7)")
* [`cmath.acosh()`](https://docs.python.org/3/library/cmath.html#cmath.acosh "(in Python v3.7)")
* [`cmath.asin()`](https://docs.python.org/3/library/cmath.html#cmath.asin "(in Python v3.7)")
* [`cmath.asinh()`](https://docs.python.org/3/library/cmath.html#cmath.asinh "(in Python v3.7)")
* [`cmath.atan()`](https://docs.python.org/3/library/cmath.html#cmath.atan "(in Python v3.7)")
* [`cmath.atanh()`](https://docs.python.org/3/library/cmath.html#cmath.atanh "(in Python v3.7)")
* [`cmath.cos()`](https://docs.python.org/3/library/cmath.html#cmath.cos "(in Python v3.7)")
* [`cmath.cosh()`](https://docs.python.org/3/library/cmath.html#cmath.cosh "(in Python v3.7)")
* [`cmath.exp()`](https://docs.python.org/3/library/cmath.html#cmath.exp "(in Python v3.7)")
* [`cmath.isfinite()`](https://docs.python.org/3/library/cmath.html#cmath.isfinite "(in Python v3.7)")
* [`cmath.isinf()`](https://docs.python.org/3/library/cmath.html#cmath.isinf "(in Python v3.7)")
* [`cmath.isnan()`](https://docs.python.org/3/library/cmath.html#cmath.isnan "(in Python v3.7)")
* [`cmath.log()`](https://docs.python.org/3/library/cmath.html#cmath.log "(in Python v3.7)")
* [`cmath.log10()`](https://docs.python.org/3/library/cmath.html#cmath.log10 "(in Python v3.7)")
* [`cmath.phase()`](https://docs.python.org/3/library/cmath.html#cmath.phase "(in Python v3.7)")
* [`cmath.polar()`](https://docs.python.org/3/library/cmath.html#cmath.polar "(in Python v3.7)")
* [`cmath.rect()`](https://docs.python.org/3/library/cmath.html#cmath.rect "(in Python v3.7)")
* [`cmath.sin()`](https://docs.python.org/3/library/cmath.html#cmath.sin "(in Python v3.7)")
* [`cmath.sinh()`](https://docs.python.org/3/library/cmath.html#cmath.sinh "(in Python v3.7)")
* [`cmath.sqrt()`](https://docs.python.org/3/library/cmath.html#cmath.sqrt "(in Python v3.7)")
* [`cmath.tan()`](https://docs.python.org/3/library/cmath.html#cmath.tan "(in Python v3.7)")
* [`cmath.tanh()`](https://docs.python.org/3/library/cmath.html#cmath.tanh "(in Python v3.7)")
### 2.6.4.3。 `collections`
由 [`collections.namedtuple()`](https://docs.python.org/3/library/collections.html#collections.namedtuple "(in Python v3.7)") 返回的命名元組類的支持方式與支持常規元組的方式相同。還支持構造函數中的屬性訪問和命名參數。
在 Numba 代碼中創建一個命名的元組類是 _ 而不是 _ 支持;必須在全局級別創建該類。
### 2.6.4.4。 `ctypes`
Numba 能夠使用以下參數和返回類型調用 ctypes 聲明的函數:
* [`ctypes.c_int8`](https://docs.python.org/3/library/ctypes.html#ctypes.c_int8 "(in Python v3.7)")
* [`ctypes.c_int16`](https://docs.python.org/3/library/ctypes.html#ctypes.c_int16 "(in Python v3.7)")
* [`ctypes.c_int32`](https://docs.python.org/3/library/ctypes.html#ctypes.c_int32 "(in Python v3.7)")
* [`ctypes.c_int64`](https://docs.python.org/3/library/ctypes.html#ctypes.c_int64 "(in Python v3.7)")
* [`ctypes.c_uint8`](https://docs.python.org/3/library/ctypes.html#ctypes.c_uint8 "(in Python v3.7)")
* [`ctypes.c_uint16`](https://docs.python.org/3/library/ctypes.html#ctypes.c_uint16 "(in Python v3.7)")
* [`ctypes.c_uint32`](https://docs.python.org/3/library/ctypes.html#ctypes.c_uint32 "(in Python v3.7)")
* [`ctypes.c_uint64`](https://docs.python.org/3/library/ctypes.html#ctypes.c_uint64 "(in Python v3.7)")
* [`ctypes.c_float`](https://docs.python.org/3/library/ctypes.html#ctypes.c_float "(in Python v3.7)")
* [`ctypes.c_double`](https://docs.python.org/3/library/ctypes.html#ctypes.c_double "(in Python v3.7)")
* [`ctypes.c_void_p`](https://docs.python.org/3/library/ctypes.html#ctypes.c_void_p "(in Python v3.7)")
### 2.6.4.5。 `enum`
支持 [`enum.Enum`](https://docs.python.org/3/library/enum.html#enum.Enum "(in Python v3.7)") 和 [`enum.IntEnum`](https://docs.python.org/3/library/enum.html#enum.IntEnum "(in Python v3.7)") 子類。
### 2.6.4.6。 `math`
支持 [`math`](https://docs.python.org/3/library/math.html#module-math "(in Python v3.7)") 模塊的以下功能:
* [`math.acos()`](https://docs.python.org/3/library/math.html#math.acos "(in Python v3.7)")
* [`math.acosh()`](https://docs.python.org/3/library/math.html#math.acosh "(in Python v3.7)")
* [`math.asin()`](https://docs.python.org/3/library/math.html#math.asin "(in Python v3.7)")
* [`math.asinh()`](https://docs.python.org/3/library/math.html#math.asinh "(in Python v3.7)")
* [`math.atan()`](https://docs.python.org/3/library/math.html#math.atan "(in Python v3.7)")
* [`math.atan2()`](https://docs.python.org/3/library/math.html#math.atan2 "(in Python v3.7)")
* [`math.atanh()`](https://docs.python.org/3/library/math.html#math.atanh "(in Python v3.7)")
* [`math.ceil()`](https://docs.python.org/3/library/math.html#math.ceil "(in Python v3.7)")
* [`math.copysign()`](https://docs.python.org/3/library/math.html#math.copysign "(in Python v3.7)")
* [`math.cos()`](https://docs.python.org/3/library/math.html#math.cos "(in Python v3.7)")
* [`math.cosh()`](https://docs.python.org/3/library/math.html#math.cosh "(in Python v3.7)")
* [`math.degrees()`](https://docs.python.org/3/library/math.html#math.degrees "(in Python v3.7)")
* [`math.erf()`](https://docs.python.org/3/library/math.html#math.erf "(in Python v3.7)")
* [`math.erfc()`](https://docs.python.org/3/library/math.html#math.erfc "(in Python v3.7)")
* [`math.exp()`](https://docs.python.org/3/library/math.html#math.exp "(in Python v3.7)")
* [`math.expm1()`](https://docs.python.org/3/library/math.html#math.expm1 "(in Python v3.7)")
* [`math.fabs()`](https://docs.python.org/3/library/math.html#math.fabs "(in Python v3.7)")
* [`math.floor()`](https://docs.python.org/3/library/math.html#math.floor "(in Python v3.7)")
* [`math.frexp()`](https://docs.python.org/3/library/math.html#math.frexp "(in Python v3.7)")
* [`math.gamma()`](https://docs.python.org/3/library/math.html#math.gamma "(in Python v3.7)")
* [`math.hypot()`](https://docs.python.org/3/library/math.html#math.hypot "(in Python v3.7)")
* [`math.isfinite()`](https://docs.python.org/3/library/math.html#math.isfinite "(in Python v3.7)")
* [`math.isinf()`](https://docs.python.org/3/library/math.html#math.isinf "(in Python v3.7)")
* [`math.isnan()`](https://docs.python.org/3/library/math.html#math.isnan "(in Python v3.7)")
* [`math.ldexp()`](https://docs.python.org/3/library/math.html#math.ldexp "(in Python v3.7)")
* [`math.lgamma()`](https://docs.python.org/3/library/math.html#math.lgamma "(in Python v3.7)")
* [`math.log()`](https://docs.python.org/3/library/math.html#math.log "(in Python v3.7)")
* [`math.log10()`](https://docs.python.org/3/library/math.html#math.log10 "(in Python v3.7)")
* [`math.log1p()`](https://docs.python.org/3/library/math.html#math.log1p "(in Python v3.7)")
* [`math.pow()`](https://docs.python.org/3/library/math.html#math.pow "(in Python v3.7)")
* [`math.radians()`](https://docs.python.org/3/library/math.html#math.radians "(in Python v3.7)")
* [`math.sin()`](https://docs.python.org/3/library/math.html#math.sin "(in Python v3.7)")
* [`math.sinh()`](https://docs.python.org/3/library/math.html#math.sinh "(in Python v3.7)")
* [`math.sqrt()`](https://docs.python.org/3/library/math.html#math.sqrt "(in Python v3.7)")
* [`math.tan()`](https://docs.python.org/3/library/math.html#math.tan "(in Python v3.7)")
* [`math.tanh()`](https://docs.python.org/3/library/math.html#math.tanh "(in Python v3.7)")
* [`math.trunc()`](https://docs.python.org/3/library/math.html#math.trunc "(in Python v3.7)")
### 2.6.4.7。 `operator`
支持 [`operator`](https://docs.python.org/3/library/operator.html#module-operator "(in Python v3.7)") 模塊的以下功能:
* [`operator.add()`](https://docs.python.org/3/library/operator.html#operator.add "(in Python v3.7)")
* [`operator.and_()`](https://docs.python.org/3/library/operator.html#operator.and_ "(in Python v3.7)")
* `operator.div()`(僅限 Python 2)
* [`operator.eq()`](https://docs.python.org/3/library/operator.html#operator.eq "(in Python v3.7)")
* [`operator.floordiv()`](https://docs.python.org/3/library/operator.html#operator.floordiv "(in Python v3.7)")
* [`operator.ge()`](https://docs.python.org/3/library/operator.html#operator.ge "(in Python v3.7)")
* [`operator.gt()`](https://docs.python.org/3/library/operator.html#operator.gt "(in Python v3.7)")
* [`operator.iadd()`](https://docs.python.org/3/library/operator.html#operator.iadd "(in Python v3.7)")
* [`operator.iand()`](https://docs.python.org/3/library/operator.html#operator.iand "(in Python v3.7)")
* `operator.idiv()`(僅限 Python 2)
* [`operator.ifloordiv()`](https://docs.python.org/3/library/operator.html#operator.ifloordiv "(in Python v3.7)")
* [`operator.ilshift()`](https://docs.python.org/3/library/operator.html#operator.ilshift "(in Python v3.7)")
* [`operator.imatmul()`](https://docs.python.org/3/library/operator.html#operator.imatmul "(in Python v3.7)") (Python 3.5 及以上版本)
* [`operator.imod()`](https://docs.python.org/3/library/operator.html#operator.imod "(in Python v3.7)")
* [`operator.imul()`](https://docs.python.org/3/library/operator.html#operator.imul "(in Python v3.7)")
* [`operator.invert()`](https://docs.python.org/3/library/operator.html#operator.invert "(in Python v3.7)")
* [`operator.ior()`](https://docs.python.org/3/library/operator.html#operator.ior "(in Python v3.7)")
* [`operator.ipow()`](https://docs.python.org/3/library/operator.html#operator.ipow "(in Python v3.7)")
* [`operator.irshift()`](https://docs.python.org/3/library/operator.html#operator.irshift "(in Python v3.7)")
* [`operator.isub()`](https://docs.python.org/3/library/operator.html#operator.isub "(in Python v3.7)")
* [`operator.itruediv()`](https://docs.python.org/3/library/operator.html#operator.itruediv "(in Python v3.7)")
* [`operator.ixor()`](https://docs.python.org/3/library/operator.html#operator.ixor "(in Python v3.7)")
* [`operator.le()`](https://docs.python.org/3/library/operator.html#operator.le "(in Python v3.7)")
* [`operator.lshift()`](https://docs.python.org/3/library/operator.html#operator.lshift "(in Python v3.7)")
* [`operator.lt()`](https://docs.python.org/3/library/operator.html#operator.lt "(in Python v3.7)")
* [`operator.matmul()`](https://docs.python.org/3/library/operator.html#operator.matmul "(in Python v3.7)") (Python 3.5 及以上版本)
* [`operator.mod()`](https://docs.python.org/3/library/operator.html#operator.mod "(in Python v3.7)")
* [`operator.mul()`](https://docs.python.org/3/library/operator.html#operator.mul "(in Python v3.7)")
* [`operator.ne()`](https://docs.python.org/3/library/operator.html#operator.ne "(in Python v3.7)")
* [`operator.neg()`](https://docs.python.org/3/library/operator.html#operator.neg "(in Python v3.7)")
* [`operator.not_()`](https://docs.python.org/3/library/operator.html#operator.not_ "(in Python v3.7)")
* [`operator.or_()`](https://docs.python.org/3/library/operator.html#operator.or_ "(in Python v3.7)")
* [`operator.pos()`](https://docs.python.org/3/library/operator.html#operator.pos "(in Python v3.7)")
* [`operator.pow()`](https://docs.python.org/3/library/operator.html#operator.pow "(in Python v3.7)")
* [`operator.rshift()`](https://docs.python.org/3/library/operator.html#operator.rshift "(in Python v3.7)")
* [`operator.sub()`](https://docs.python.org/3/library/operator.html#operator.sub "(in Python v3.7)")
* [`operator.truediv()`](https://docs.python.org/3/library/operator.html#operator.truediv "(in Python v3.7)")
* [`operator.xor()`](https://docs.python.org/3/library/operator.html#operator.xor "(in Python v3.7)")
### 2.6.4.8。 `functools`
支持 [`functools.reduce()`](https://docs.python.org/3/library/functools.html#functools.reduce "(in Python v3.7)") 功能,但需要<cite>初始值設定項</cite>參數。
### 2.6.4.9。 `random`
Numba 支持 [`random`](https://docs.python.org/3/library/random.html#module-random "(in Python v3.7)") 模塊的頂級功能,但不允許您創建單獨的 Random 實例。使用 Mersenne-Twister 發電機,具有專用的內部狀態。它在啟動時初始化,并從操作系統中提取熵。
* [`random.betavariate()`](https://docs.python.org/3/library/random.html#random.betavariate "(in Python v3.7)")
* [`random.expovariate()`](https://docs.python.org/3/library/random.html#random.expovariate "(in Python v3.7)")
* [`random.gammavariate()`](https://docs.python.org/3/library/random.html#random.gammavariate "(in Python v3.7)")
* [`random.gauss()`](https://docs.python.org/3/library/random.html#random.gauss "(in Python v3.7)")
* [`random.getrandbits()`](https://docs.python.org/3/library/random.html#random.getrandbits "(in Python v3.7)") :位數不得大于 64
* [`random.lognormvariate()`](https://docs.python.org/3/library/random.html#random.lognormvariate "(in Python v3.7)")
* [`random.normalvariate()`](https://docs.python.org/3/library/random.html#random.normalvariate "(in Python v3.7)")
* [`random.paretovariate()`](https://docs.python.org/3/library/random.html#random.paretovariate "(in Python v3.7)")
* [`random.randint()`](https://docs.python.org/3/library/random.html#random.randint "(in Python v3.7)")
* [`random.random()`](https://docs.python.org/3/library/random.html#random.random "(in Python v3.7)")
* [`random.randrange()`](https://docs.python.org/3/library/random.html#random.randrange "(in Python v3.7)")
* [`random.seed()`](https://docs.python.org/3/library/random.html#random.seed "(in Python v3.7)") :僅帶整數參數
* [`random.shuffle()`](https://docs.python.org/3/library/random.html#random.shuffle "(in Python v3.7)") :序列參數必須是一維 Numpy 數組或緩沖區提供對象(例如 [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray "(in Python v3.7)") 或 [`array.array`](https://docs.python.org/3/library/array.html#array.array "(in Python v3.7)") ) ;不支持第二個(可選)參數
* [`random.uniform()`](https://docs.python.org/3/library/random.html#random.uniform "(in Python v3.7)")
* [`random.triangular()`](https://docs.python.org/3/library/random.html#random.triangular "(in Python v3.7)")
* [`random.vonmisesvariate()`](https://docs.python.org/3/library/random.html#random.vonmisesvariate "(in Python v3.7)")
* [`random.weibullvariate()`](https://docs.python.org/3/library/random.html#random.weibullvariate "(in Python v3.7)")
注意
從非 Numba 代碼(或從[對象模式](../glossary.html#term-object-mode)代碼)調用 [`random.seed()`](https://docs.python.org/3/library/random.html#random.seed "(in Python v3.7)") 將播種 Python 隨機生成器,而不是 Numba 隨機生成器。
注意
從版本 0.28.0 開始,發生器是線程安全的和叉安全的。每個線程和每個進程將產生獨立的隨機數流。
也可以看看
Numba 還支持來自 [Numpy 隨機模塊](numpysupported.html#numpy-random)的大多數其他發行版。
### 2.6.4.10。 `heapq`
支持 [`heapq`](https://docs.python.org/3/library/heapq.html#module-heapq "(in Python v3.7)") 模塊的以下功能:
* [`heapq.heapify()`](https://docs.python.org/3/library/heapq.html#heapq.heapify "(in Python v3.7)")
* [`heapq.heappop()`](https://docs.python.org/3/library/heapq.html#heapq.heappop "(in Python v3.7)")
* [`heapq.heappush()`](https://docs.python.org/3/library/heapq.html#heapq.heappush "(in Python v3.7)")
* [`heapq.heappushpop()`](https://docs.python.org/3/library/heapq.html#heapq.heappushpop "(in Python v3.7)")
* [`heapq.heapreplace()`](https://docs.python.org/3/library/heapq.html#heapq.heapreplace "(in Python v3.7)")
* [`heapq.nlargest()`](https://docs.python.org/3/library/heapq.html#heapq.nlargest "(in Python v3.7)") :僅前兩個參數
* [`heapq.nsmallest()`](https://docs.python.org/3/library/heapq.html#heapq.nsmallest "(in Python v3.7)") :僅前兩個參數
注意:必須為堆添加至少一個值以允許其類型被推斷;假設堆項目在類型上是同類的。
## 2.6.5。第三方模塊
### 2.6.5.1。 `cffi`
與 ctypes 類似,Numba 能夠使用以下 C 類型和任何派生指針類型調用 [cffi](https://cffi.readthedocs.org/) - 聲明的外部函數:
* `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`
* `ssize_t`
* `size_t`
* `void`
`cffi.FFI`和`CompiledFFI`對象的`from_buffer()`方法支持傳遞 Numpy 數組和其他類似緩沖區的對象。只接受 _ 連續 _ 參數。 `from_buffer()`的參數被轉換為適當 C 類型的原始指針(例如`float64`數組的`double *`)。
可以向 Numba 注冊用于從緩沖區到適當的 C 類型的轉換的附加類型映射。這可能包括結構類型,但它只允許調用接受結構指針的函數 - 不支持按值傳遞結構。要注冊映射,請使用:
```py
numba.cffi_support.register_type(cffi_type, numba_type)
```
在使用 Numba 編譯的函數中的任何函數之前,必須在 Numba 中注冊外部 cffi 模塊:
```py
numba.cffi_support.register_module(mod)
```
使用 Numba 注冊 cffi 外線模塊`mod`。
內聯 cffi 模塊不需要注冊。
- 1. 用戶手冊
- 1.1。 Numba 的約 5 分鐘指南
- 1.2。概述
- 1.3。安裝
- 1.4。使用@jit 編譯 Python 代碼
- 1.5。使用@generated_jit 進行靈活的專業化
- 1.6。創建 Numpy 通用函數
- 1.7。用@jitclass 編譯 python 類
- 1.8。使用@cfunc 創建 C 回調
- 1.9。提前編譯代碼
- 1.10。使用@jit 自動并行化
- 1.11。使用@stencil裝飾器
- 1.12。從 JIT 代碼 中回調到 Python 解釋器
- 1.13。性能提示
- 1.14。線程層
- 1.15。故障排除和提示
- 1.16。常見問題
- 1.17。示例
- 1.18。會談和教程
- 2. 參考手冊
- 2.1。類型和簽名
- 2.2。即時編譯
- 2.3。提前編譯
- 2.4。公用事業
- 2.5。環境變量
- 2.6。支持的 Python 功能
- 2.7。支持的 NumPy 功能
- 2.8。與 Python 語義的偏差
- 2.9。浮點陷阱
- 2.10。 Python 2.7 壽命終止計劃
- 3. 用于 CUDA GPU 的 Numba
- 3.1。概述
- 3.2。編寫 CUDA 內核
- 3.3。內存管理
- 3.4。編寫設備功能
- 3.5。 CUDA Python 中支持的 Python 功能
- 3.6。支持的原子操作
- 3.7。隨機數生成
- 3.8。設備管理
- 3.10。示例
- 3.11。使用 CUDA 模擬器 調試 CUDA Python
- 3.12。 GPU 減少
- 3.13。 CUDA Ufuncs 和廣義 Ufuncs
- 3.14。共享 CUDA 內存
- 3.15。 CUDA 陣列接口
- 3.16。 CUDA 常見問題
- 4. CUDA Python 參考
- 4.1。 CUDA 主機 API
- 4.2。 CUDA 內核 API
- 4.3。內存管理
- 5. 用于 AMD ROC GPU 的 Numba
- 5.1。概述
- 5.2。編寫 HSA 內核
- 5.3。內存管理
- 5.4。編寫設備功能
- 5.5。支持的原子操作
- 5.6。代理商
- 5.7。 ROC Ufuncs 和廣義 Ufuncs
- 5.8。示例
- 6. 擴展 Numba
- 6.1。高級擴展 API
- 6.2。低級擴展 API
- 6.3。示例:間隔類型
- 7. 開發者手冊
- 7.1。貢獻給 Numba
- 7.2。 Numba 建筑
- 7.3。多態調度
- 7.4。關于發電機的注意事項
- 7.5。關于 Numba Runtime 的注意事項
- 7.6。使用 Numba Rewrite Pass 獲得樂趣和優化
- 7.7。實時變量分析
- 7.8。上市
- 7.9。模板注釋
- 7.10。關于自定義管道的注意事項
- 7.11。環境對象
- 7.12。哈希 的注意事項
- 7.13。 Numba 項目路線圖
- 8. Numba 增強建議
- 9. 術語表