numba

NG python

import sys
import numba


def recur(t):
    if t == 0:
        return 1
    return t * recur(t - 1)


if sys.argv[-1] == "-c":
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('recur', 'i8(i8)')(recur)
    cc.compile()
else:
    from my_module import recur
    print(recur(5))

:

Untyped global name 'recur': cannot determine Numba type of <class 'function'>

File "numbatest.py", line 7:
def recur(t):
    <source elided>
        return 1
    return t * recur(t - 1)
    ^

OK python

@numba.njit("i8(i8)")
def recur(t):

NG python

def main(x):
    def recur(t):

Not Supported

Numba now supports inner functions as long as they are non-recursive and only called locally, but not passed as argument or returned as result. The use of closure variables (variables defined in outer scopes) within an inner function is also supported.


This page is auto-translated from [/nishio/numba recursion](https://scrapbox.io/nishio/numba recursion) using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I’m very happy to spread my thought to non-Japanese readers.