Created
October 23, 2019 09:42
-
-
Save FedericoStra/817d45095e0f4129ed54459488b5bd3c to your computer and use it in GitHub Desktop.
Usage of global variables with numba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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