I’m working with PyROOT on a Notebook environment. Let’s say I’ve created a function and compiled it with ROOT.Numba, e.g. entering something below in a code cell:
Very often, the function is not yet final. I may need to modify it several more times.
However, when I try to re-run the code cell, I would get a redefinition error like below:
input_line_686:5:20: error: redefinition of 'myfunc'
float myfunc(float x, int y) {
I understand that this behavior is totally expected because the Notebook is still in the same Python session, meaning that it “remembers” the declared functions. So unless I restart the kernel every time I modify the numba function, which is really time-consuming, I couldn’t really find a workaround for this matter.
My question: Is there a way to “delete” the functions compiled with ROOT.Numba? Thanks.
**Simiarly, when declaring variables/functions using ROOT.gInterpreter.Declare(), similar issue of redefinition will happen. I have tried doing gROOT.Reset() and realized that didn’t quite help.
I believe at the moment there is no way to get rid of that function definition. A shadowing mechanism to allow redefinitions has been implemented in ROOT (which is useful for interactive environments such as notebooks). However, the case we have here - a function in a namespace - does not seem to be supported.
There is an alternative in your case:
id = 0
ROOT.Numba.Declare(['float'], ['int', 'float'], name='f' + str(id))
def func(x, y):
return x * y
id += 1
You can provide a different name every time. Not very pretty, but it should work.
First of all, thanks for reporting. Based on the information provided by @etejedor, a function defined via ROOT.Numba.Declare() is currently declared as a member of a namespace. However, definition shadowing within the Cling C++ interpreter is only enabled for global declarations. This essentially means that the current implementation does not allow members of a namespace to be redefined.
Whether this should be supported or not is something that we should discuss, as supporting namespaces might require a number of changes to the underlying implementation. For reference, I am also tagging @Axel in this post.
I didn’t know there’s a name option for the Declare() function. Just tested it on my notebook, and it works!
In view of the other replies, it seems that there’s currently no way to redefine a namespace in the Cling C++ interpreter. So I have marked your reply as the solution.