为什么在 python 中修改局部变量是个坏主意?
Related to this reply here. Locals' doc here.
The docs mention that the dictionary should not change, not sure what it means but would locals()
be applicable in lab-reports where data won't change, for example in measurements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
文档所说的是,当您有一个本地
x
变量并执行locals()['x'] = 42
时,那么x
可能仍然指向旧对象。What documentation says is that when you have a local
x
variable and dolocals()['x'] = 42
, thenx
may still point to the old object.在某些情况下,对 locals() 的调用返回从多个源收集的值,而不是指向本地范围的指针。
示例:在函数调用内部时,locals() 返回全局作用域和函数局部作用域的组合。在这种情况下,修改 locals() 输出不会对本地范围进行任何更改,因为它本质上是使用一个岛。似乎它唯一起作用的情况是其输出与 globals() 的输出相同的情况。
因此,换句话说,您要么想使用 globals(),要么找到不同的方法来实现相同的目标。
In certain cases, the call to locals() returns values collected from multiple sources, rather than a pointer to the local scope.
Example: When inside a function call, locals() returns a combination of the global scope and the scope local to the function. In this case, modifying the locals() output won't make any changes to the local scope because it's essentially using an island. It seems like the only cases where it does work are cases where its output is the same as the output of globals().
So, in other words, you either want to use globals(), or find a different way to achieve the same goal.
在 CPython 解释器中,局部变量可以来自多个地方(细节并不重要,但它与闭包的变量存储方式有关)。
locals()
函数从所有这些位置收集名称和值,以便您可以在一个位置方便地访问它们,但由于它不知道给定变量来自哪里,因此它无法放回去。换句话说,这是一个坏主意,因为它不起作用。In the CPython interpreter, local variables can come from a number of places (the details of this are not important, but it has to do with how variables are stored for closures). The
locals()
function gathers up the names and values from all these places, to give you convenient access to them all in one place, but since it doesn't know where a given variable came from, it can't put it back. In other words, it's a bad idea because it doesn't work.来自深入了解 Python
From Dive into Python
修改是一个坏主意,因为文档(您链接的)明确表示不要:
你不需要更多的理由。
如果您以不修改任何变量的方式使用它,那么您会没事的,但我会质疑设计并看看是否有更好的方法来完成您想要的事情。
在您链接的具体示例中,locals 实际上是 globals(),因为您在模块的全局范围内使用它。这种非常具体的用法现在可以工作,尽管我希望它能像全局变量一样继续工作,但您也可以只使用全局变量。
一个更干净的解决方案可能是,在不知道设计的其余部分的情况下,为变量使用常规的字典;然后使用 data["x"] = value 而不是 globals()["x"] = value。
Modification is a bad idea because the documentation (which you link) explicitly says not to:
You don't need any more reason than that.
If you are using it in a way that doesn't modify any variables, then you'll be fine, but I'd question the design and see if there's a better way to do what you want.
In the specific example you link, locals is actually globals(), as you use it in the global scope of a module. This very specific use works now and, though I expect it to continue to work just as with globals, you might as well just use globals instead.
An even cleaner solution is probably, without knowing the rest of your design, to use a regular ol' dictionary for your variables; then use data["x"] = value instead of globals()["x"] = value.