numba/numba — pull request #450 by seibert (2014-05-26 23:29:04+00:00)
Title: Allow JIT to see variables from enclosing, non-global scopes

The ability to reference variables (and functions) from function closures is very handy, especially if you want to generate JIT functions composed with helper functions passed in by the user. For example, this would now be possible:

```python
import numpy as np
import numba

def make_jit_func(transform):
c_transform = numba.jit('f8(f8)', nopython=True)(transform)

def sum_with_transform(ary):
s = 0.0
for i in range(ary.shape[0]):
s += c_transform(ary[i])
return s

return numba.jit('f8(f8[:])')(sum_with_transform)

def my_transform(x):
return 1 + x ** 2
# Generate a custom JIT summation function with my transform embedded
sum_with_transform = make_jit_func(my_transform)

ary = np.arange(10, dtype=np.float64)

print(sum_with_transform(ary))
```

The semantics are the same as for globals: The value and type of the non-local variable are captured at the time of JIT compilation.