为什么我要评估两次? [英] Why do I have to evaluate this twice?

查看:36
本文介绍了为什么我要评估两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我必须对这个任务进行两次评估(在 Mathematica 7 中).

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

第一次评估:

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

*)

Out[2]= {SequenceHold} 中可以看出,Unprotect[Rule] 有效,但错误消息表明并非如此.如果我第二次评估该单元格,则会进行分配并且不会产生错误.

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?

推荐答案

您可能知道,Mathematica 加载实现其某些功能的二进制 MX 文件.这些 MX 文件存储实现以及定义和属性.

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.

这是阴险的,但是您的 Unprotect[Rule] 被 Mathematica 新加载的 MX 文件撤消,这解释了为什么它第二次起作用.因为 Mathematica 已经加载了它需要的所有 MX 文件.

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 在取消保护 Rule 之前,需要进行第一次评估以触发所有自动加载.

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

这篇关于为什么我要评估两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆