The chunk option cache
doesn't save all the variables defined in the block for languages other than R
. It is, though, saving printed outputs, so if you compute something that takes a while, any results will not need to be re-computed. From the knitr
website:
Except engine='R'
(default), all chunks are executed in separate
sessions, so the variables cannot be directly shared. If we want to
make use of objects created in previous chunks, we usually have to
write them to files (as side effects). For the bash engine, we can use
Sys.setenv()
to export variables from R to bash (example).
It's possible to save a few values in the shell's environment, and retrieve those values from the other cells by reading the environment. This is the approach Yihui took in the Polyglot example. So, for Python, if you can format the value as a string and pass it to sys.setenv()
, you could use that value in another cell (run as a separate Python session) by calling sys.getenv()
.
Though, I am mildly confused about the approach taken with the C
and Fortran
engines. Those seem to have access to compiled functions in later chunks by using some function called .C()
or a function called .Fortran()
. But it seems that Python does not have an equivalent.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…