Why do I have to evaluate this twice?

◇◆丶佛笑我妖孽 提交于 2019-12-17 23:17:56

问题


I cannot figure out why I have to evaluate this twice (in Mathematica 7) for the assignment to take.

First evaluation:

Unprotect[Rule];
Attributes[Rule]
pp = Plot | LogLinearPlot | ListPlot | ParametricPlot3D;
(h : pp)[True -> False] ^:= Print["Irrelevant data"]

(*

Out[2]= {SequenceHold}

During evaluation of In[1]:= UpSetDelayed::write: Tag Rule in (h:Plot|LogLinearPlot|ListPlot|ParametricPlot3D)[True->False] is Protected. >>

Out[4]= $Failed

*)

As can be seen from Out[2]= {SequenceHold}, Unprotect[Rule] worked, yet the error message indicates otherwise. If I evaluate the cell a second time, the assignment takes and no error is generated.

Why does this happen?


回答1:


As you might well know, Mathematica loads binary MX files that implement some of its functionality. These MX files store implementations as well as definitions and attributes.

This is insidious, but your Unprotect[Rule] is undone by Mathematica's newly loaded MX file, and this explains why it worked the second time. Because Mathematica had already loaded all MX files it needed.

If you first evaluate all the symbols in your expression, then it stops complaining:

{Unprotect, Rule, Attributes, Plot, LogLinearPlot, ListPlot, 
  ParametricPlot3D, True, False, Print};
Unprotect[Rule];
Attributes[Rule];
pp = Plot | LogLinearPlot | ListPlot | ParametricPlot3D;
(h : pp)[True -> False] ^:= Print["Irrelevant data"]

EDIT The first evaluation is needed to trigger all the auto-loading before you unprotect Rule.



来源:https://stackoverflow.com/questions/5649379/why-do-i-have-to-evaluate-this-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!