意外阴影和`移除[符号]` [英] Accidental shadowing and `Removed[symbol]`

查看:24
本文介绍了意外阴影和`移除[符号]`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您对以下代码进行两次评估,结果将有所不同.谁能解释一下这是怎么回事?

If you evaluate the following code twice, results will be different. Can anyone explain what's going on?

findHull[points_] := Module[{},
   Needs["ComputationalGeometry`"];
   ConvexHull[points]
   ];
findHull[RandomReal[1, {10, 2}]];
Remove["Global`ConvexHull"];
findHull[RandomReal[1, {10, 2}]]

推荐答案

问题是,即使模块在调用 findHull 之前不会被求值,但是当你定义 时符号会被解析findHull(即:findHull 的新下值以符号而非文本形式存储).这意味着在第一轮中,ConvexHull 解析为 Global`ConvexHull,因为 Needs 没有被评估.在第二轮中,ComputationalGeometry 位于 $ContextPath 上,因此 ConvexHull 按您的预期解析.

The problem is that even though the module is not evaluated untill you call findHull, the symbols are resolved when you define findHull (i.e.: The new downvalue for findHull is stored in terms of symbols, not text). This means that during the first round, ConvexHull resolves to Global`ConvexHull because the Needs is not evaluated. During the second round, ComputationalGeometry is on $ContextPath and so ConvexHull resolves as you intended.

如果您实在不忍心事先加载ComputationalGeometry,只需参考ConvexHull 的全名:ComputationalGeometry`ConvexHull.另请参阅此相关答案.

If you really cannot bear to load ComputationalGeometry beforehand, just refer to ConvexHull by its full name: ComputationalGeometry`ConvexHull. See also this related answer.

HTH

这篇关于意外阴影和`移除[符号]`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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