I am quite confused about Context. I have a long semi-manual signal processing task. I already did that for one signal and I need to do it for another one. I want to preserve all variables for both signals as their calculation is lengthy. So I thought of using Context for that (else, I would need to make new variables with new names and that would mean to change all the code.
Now, if I do the following sequnce of commands:
$Context
returns:
Out[1]= "Global`"
Starting a new context:
Begin["ContextA`"]
a = 3;
b = 33;
c = a + b;
{a, b, c}
End[]
returns:
"ContextA`"
{3, 33, 36}
"ContextA`"
Which context am I now in?
$Context
returns
Out[8]= "Global`"
Try:
a
returns:
Out[9]= a
Try
{ContextA`a, ContextA`b, ContextA`c}
returns
Out[11]= {3, 33, 36}
Up to now everything is as expected. But the problem starts when I want to go back to the context ContextA:
Begin["ContextA`"]
$Context
returns:
"ContextA`"
"ContextA`"
Correct but:
a
returns unexpectedly
Out[14]= a
Then
b
Makes more confusion by returning:
Out[15]= 33
the rest is as I was expecting:
c
returns
Out[16]= 36
So let's try again
a
returns
Out[17]= a
try something else
d = 123;
End[]
gives
Out[19]= "ContextA`"
Verify, i am out:
In[20]:= $Context
Out[20]= "Global`"
Where is d?
In[21]:= d
Out[21]= d
In[23]:= {ContextAa, ContextAb, ContextAc, ContextAd}
Out[23]= {3, 33, 36, 123}
Try again:
In[24]:= Begin["ContextA`"]
$Context
Out[24]= "ContextA`"
Out[25]= "ContextA`"
In[26]:= a
Out[26]= a
In[27]:= ContextA`a
Out[27]= 3
What is wrong with this? Why cannot I access a when I switch to the ContextA? What is the proper way to switching back to a given context to continue to work in that context so that I have access to all expressions that i previously created in that context?
At this point, I read the suggested posts this and this which seem to indicated that the way a behaves should be correct, but then, why the other variables are not behaving the same way?
Anyway, if I had to prepend ContextA to all variables after entering the context, then this is completely useless for my purpose as it is the same as having to rename all the variables. Is there a way to tell Mathematica, "Hey, now we are working with variables in ContextA so when I write a, you will interpret that as "ContextA`a"!"?
ain the context that was in theContextPathbut the others I did not. Now a followup, not sure how to ask: Why does typingaand executing "create"a. What does that even mean in this sense to "create". I understand (though superficially) things likea=1ora:=betc there is assignment butadoes not assign anything, so why suddenly it "pops up" into the memory world? – atapaka Apr 05 '23 at 14:59MakeExpressionto turn this into a symbolic expression to be evaluated (strictly speaking, this is the place where symbols are created, effectively using something likeSymbol). This allows the kernel to uniformly handle any input: It will always be a combination of numbers, strings & symbols, each symbol having a name and a context. [cont.] – Lukas Lang Apr 05 '23 at 16:02aneeds to create the symbola. Maybe it also helps to think about a slightly less extreme example likelist = {a,b}- even without any assignment, bothaandbneed to exist as symbols. Otherwise, what wouldlistcontain? Or takeContext[a]orSymbolName[a], where clearly,aneeds to be a symbol. – Lukas Lang Apr 05 '23 at 16:03MakeExpressionstep (for notebooks)/the parsing step (for package files). Only then is the code evaluated and any assignments etc. are performed (you'll note that this is a completely separate step from symbol creation). – Lukas Lang Apr 05 '23 at 16:05