在 Mathematica 中使用 $NewSymbol 移动上下文 [英] Moving contexts with $NewSymbol in Mathematica
问题描述
我在玩 $NewSymbol
试图找到一些东西来回答这个问题.文档说
I was playing around with $NewSymbol
trying to find something to answer this question with. The docs say that
$NewSymbol 在符号实际上是创建的.如果$NewSymbol 的操作导致符号被创造,也许在不同的上下文,然后是创建的符号将是使用的那个.
$NewSymbol is applied before the symbol is actually created. If the action of $NewSymbol causes the symbol to be created, perhaps in a different context, then the symbol as created will be the one used.
所以我尝试自动将一个特定的新符号移动到测试上下文中,这应该会阻止它在 Global`*
上下文中的创建,但是该符号在两个上下文中都被创建.
So I tried to automatically move a particular new symbol into a test context which should prevent its creation in the Global`*
context, but the symbol gets created in both contexts.
In[1]:= Remove["Global`*"]
In[2]:= $NewSymbol=(Print[#1," : ",#2];
If[#1==="aLongTestSymbolName"&==="Global`",
Symbol["TestContext`"<>#1]])&;
In[3]:= x
During evaluation of In[3]:= x : Global`
Out[3]= x
In[4]:= aLongTestSymbolName
During evaluation of In[4]:= aLongTestSymbolName : Global`
During evaluation of In[4]:= aLongTestSymbolName : TestContext`
Out[4]= aLongTestSymbolName
In[5]:= Names["Global`*"]
Out[5]= {aLongTestSymbolName,x}
In[6]:= Names["TestContext`*"]
Out[6]= {TestContext`aLongTestSymbolName}
我认为 "aLongTestSymbolName"
不应该出现在 Global`
上下文中.谁能看到我做错了什么,或者我是否误解了文档?
I believe that "aLongTestSymbolName"
should not be in the Global`
context.
Can anyone see what I've done wrong or if I've misinterpreted the documentation?
注意:在两个上下文中创建符号不是在上面链接到问题的自动突出显示的选项.这个想法是为变量保留某些符号名称,例如 "x"~~___
为变量和 "f"~~___
为函数,然后在 中使用字符串模式$NewSymbol
将符号移动到适当的突出显示的上下文中.
Note: Having the symbol created in both contexts is not an option for the automatic highlighting in the above linked to question. The idea is to reserve certain symbol names such as "x"~~___
for variables and "f"~~___
for functions and then use string patterns in $NewSymbol
to move the symbols to the appropriate highlighted context.
推荐答案
那是因为你把符号名传给了Print
,它立即在Global`
中制作了符号.:-)
That is because you passed the symbol name to Print
, which immediately made the symbol in Global`
. :-)
或者不是.我真的应该在回答之前尝试一些事情,但我想我知道这个.糟糕.
Or not. I should really try things before answering, but I thought I knew this one. Oops.
在我看来,$NewSymbol
并没有拦截符号的创建,或者如果拦截了,如何利用它并不清楚.
It seems to me now that $NewSymbol
does not intercept the creation of a symbol, or if it does, how to make use of that is not clear.
如果使用:
$NewSymbol = Print["Name: ", #2, #] &;
然后:
In[10]:= aNewSymbol
During evaluation of In[10]:= Name: Global`aNewSymbol
Out[10]= aNewSymbol
我们看到 $NewSymbol
不像 $PrePrint
那样工作,因为它的输出不会成为表达式.
We see that $NewSymbol
does not work like $PrePrint
in that its output does not become the expression.
因此,如果我们使用:
$NewSymbol = Symbol["TestContext`" <> #] &;
aSecondSymbol
aSecondSymbol
是在 Global`
中愉快地创建的,好像什么都没有改变.
aSecondSymbol
is merrily created in Global`
as though nothing had changed.
如果 $NewSymbol
可用于指导创建符号的上下文,如文档所述,我不清楚如何做到这一点.
If $NewSymbol
can be used to direct the context in which the symbol is created, as the documentation states, it is not clear to me how this may be done.
这篇关于在 Mathematica 中使用 $NewSymbol 移动上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!