为什么在 python 中修改局部变量是个坏主意?

发布于 2024-10-17 12:28:15 字数 342 浏览 3 评论 0原文

与此回复相关此处。 Locals 的文档此处。

文档提到字典不应该更改,不确定这意味着什么,但是 locals() 是否适用于数据不会更改的实验室报告,例如测量中?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遥远的绿洲 2024-10-24 12:28:15

文档所说的是,当您有一个本地 x 变量并执行 locals()['x'] = 42 时,那么 x 可能仍然指向旧对象。

def foo():
    x = 0xABCD
    locals()['x'] = 42
    print(x)

foo()

What documentation says is that when you have a local x variable and do locals()['x'] = 42, then x may still point to the old object.

def foo():
    x = 0xABCD
    locals()['x'] = 42
    print(x)

foo()
躲猫猫 2024-10-24 12:28:15

在某些情况下,对 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.

落在眉间の轻吻 2024-10-24 12:28:15

在 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.

桃扇骨 2024-10-24 12:28:15

来自深入了解 Python

locals 是一个返回一个
字典,在这里你设置一个
该字典中的值。你可能会
认为这会改变价值
局部变量 x 变为 2,但它
没有。当地人实际上并不
返回本地命名空间,它返回
一份副本。所以改变它没有任何作用
中变量的值
本地命名空间。

From Dive into Python

locals is a function that returns a
dictionary, and here you are setting a
value in that dictionary. You might
think that this would change the value
of the local variable x to 2, but it
doesn't. locals does not actually
return the local namespace, it returns
a copy. So changing it does nothing to
the value of the variables in the
local namespace.

-小熊_ 2024-10-24 12:28:15

修改是一个坏主意,因为文档(您链接的)明确表示不要:

注意:本词典的内容不应修改;更改可能不会影响解释器使用的局部变量和自由变量的值。

你不需要更多的理由。

如果您以不修改任何变量的方式使用它,那么您会没事的,但我会质疑设计并看看是否有更好的方法来完成您想要的事情。


在您链接的具体示例中,locals 实际上是 globals(),因为您在模块的全局范围内使用它。这种非常具体的用法现在可以工作,尽管我希望它能像全局变量一样继续工作,但您也可以只使用全局变量。

一个更干净的解决方案可能是,在不知道设计的其余部分的情况下,为变量使用常规的字典;然后使用 data["x"] = value 而不是 globals()["x"] = value。

Modification is a bad idea because the documentation (which you link) explicitly says not to:

Note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文