Skip to content

Instantly share code, notes, and snippets.

@FedericoStra
Created October 23, 2019 09:42
Show Gist options
  • Save FedericoStra/817d45095e0f4129ed54459488b5bd3c to your computer and use it in GitHub Desktop.
Save FedericoStra/817d45095e0f4129ed54459488b5bd3c to your computer and use it in GitHub Desktop.
Usage of global variables with numba
from numba import jit, njit
# Define a global variable.
g = 0
# foo doesn't care about the value of g at the time of definition.
@njit
def foo():
# foo uses the global variable.
return g
# foo considers the value of g at the time of compilation.
# Note that here foo is implicitly compiled because it is called for the first time.
g = 1
print(foo()) # -> 1
# foo doesn't see changes in g.
g = 2
print(foo()) # -> 1
# You need to explicitly recompile in order to update the value of g used by foo.
foo.recompile()
print(foo()) # -> 2
# MORAL OF THE STORY:
# never change the value of global variables and you won't face surprises!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment