使用相同的本地符号在另一个模块中创建一个模块是否安全?
我发现有时能够使用自己的本地符号创建本地模块(在父模块内)很有用,这可以完成一项仅由父模块使用的小任务。当模块变大时,这很有用,并且没有充分的理由在模块外部创建较小的辅助函数,因为这些辅助函数实际上仅由该父模块需要和使用。
这是一个愚蠢的例子,其中有一个模块,其中有一个辅助模块
foo[x_] := Module[{r},
r = Module[{y, sol},
sol = First@Solve[y^2 + 3 y + 2 == 0];
y /. sol
];
x^r
]
,但上面的问题是内部模块的局部变量可能与父模块的局部变量发生冲突,因为 M 笔记本使内部模块当我执行以下操作时,模块局部变量呈红色,这告诉我我正在做一些我不应该做的事情:(我不想一直担心检查我是否为内部模块使用唯一的局部符号,即与用作父模块的本地符号不同,毕竟,它应该是本地的。在这种情况下,还必须提出不同的符号名称)
foo[x_] := Module[{r, y=0},
r = Module[{y, sol},
sol = First@Solve[y^2 + 3 y + 2 == 0];
y /. sol
];
x^r
]
请注意红色,根据帮助,这是本地范围冲突或多个上下文中的阴影。
(M 需要使用更好的颜色,很难区分多种颜色,所有红色深浅) 。
(我认为这是一个阴影警告)无论如何,它告诉我我不应该这样做,尽管我在使用这种构造时没有发现任何问题。
本例中父模块局部变量“y”的值没有被内部模块“r”的调用覆盖,这很好。
同样,我不想在 foo 之外创建一个函数,因为这个小任务仅由 foo[] 使用,不需要将其移动到全局上下文。
当然,我总是可以这样写:
foo[x_] := Module[{r, y, sol},
sol = First@Solve[y^2 + 3 y + 2 == 0];
r = y /. sol;
x^r
]
但我只是给出一个例子,这是针对大型模块的,它有助于将模块本身内部的任务分解为几个较小的任务。内部函数,内部函数是我之前在其他语言(例如 Ada/Pascal 等)中使用过的东西,这些语言具有这种构造并且对于大型程序非常有用。
我的问题是:尽管 M 给了我这个红色警告,但我只是想咨询一下这里的专家,我使用上述内容是否安全?如果我需要担心这样做,
谢谢,
I find it useful sometimes to be able to make a local module (inside a parent module) with its own local symbols, which does a small task to be used only by the parent module. This is useful when the module becomes large, and there is no good reason to make smaller helper functions OUTSIDE the module as those helper functions are really needed and used by only that one parent module.
Here is a silly example with one module, and a helper module inside it to something
foo[x_] := Module[{r},
r = Module[{y, sol},
sol = First@Solve[y^2 + 3 y + 2 == 0];
y /. sol
];
x^r
]
But the problem in the above, is that the local variables for the inner module, could conflict with local variables with the parent module, because M notebook makes the inner module local variables red when I do the following, which tells me I am doing something I am not supposed to do: (I do not want to worry all the time with checking if I am using a unique local symbol for the inner module which is different from one used as local symbols for the parent Module, after all, it is supposed to be local. And also having to come up with a different symbol name when this is the case)
foo[x_] := Module[{r, y=0},
r = Module[{y, sol},
sol = First@Solve[y^2 + 3 y + 2 == 0];
y /. sol
];
x^r
]
Notice the red coloring, which according to the help, it is local scope conflict or shadowing in multiple contexts.
(M needs to use better colors, hard to make a difference between many colors, all shades of red).
(I think it is a shadowing warniong) Either way, it tells me I am not supposed to do this, even though I did not see any problem with such construct when I used it.
Value of parent module local variable 'y' in this example did not get over-written by the call to the inner module 'r' which is good.
Again, I did not want to make a function outside foo, because this small task is only used by foo[] and no need to move it to the Global context.
Ofcourse, I could always just write:
foo[x_] := Module[{r, y, sol},
sol = First@Solve[y^2 + 3 y + 2 == 0];
r = y /. sol;
x^r
]
But I am just giving an example, and this is for large module, where it helps to break the tasks inside the module itself into even few smaller tasks. Internal functions, inside functions is something I used before in other languages such as Ada/Pascal and such which has this construct and can be useful for large programs.
My question is: Just want to check with the experts here if it is safe for me to use the above, even though M gives me this red coloring warning? and if there is something I need to worry about doing this
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,在嵌套模块中使用相同的变量是安全的,只要您不丢失它们即可。 Mathematica 将
Module
中定义的每个变量视为该模块的本地变量。局部变量是临时
,编号为variable$ModuleNumber
。您可以通过以下示例自行检查:Yes, it is safe to use the same variable in nested
Modules
as long as you don't lose track of them. Mathematica treats each variable defined in aModule
as local to that module. Local variables areTemporary
, and are numbered asvariable$ModuleNumber
. You can check this for yourself with the following example:据我所知,这是检测的一个小问题。
To the best of my knowledge this is a small issue with the detection.