如何在 Python 中按名称引用整数

发布于 2024-09-26 10:00:24 字数 491 浏览 8 评论 0原文

我想要一个引用,它读作“名称‘x’指向的任何变量”,这样它的行为如下:

>>> a = 1
>>> b = 2
>>> c = (a, b)
>>> c
(1, 2)
>>> a = 3
>>> c
(3, 2)

我知道我可以通过执行以下操作对列表做类似的事情:

>>> a = [1]
>>> b = [2]
>>> c = (a, b)
>>> c
([1], [2])
>>> a[0] = 3
>>> c
([3], [2])

但是如果分配,这很容易丢失ab 到某物而不是其元素。

有没有一种简单的方法可以做到这一点?

I want to have a a reference that reads as "whatever variable of name 'x' is pointing to" with ints so that it behaves as:

>>> a = 1
>>> b = 2
>>> c = (a, b)
>>> c
(1, 2)
>>> a = 3
>>> c
(3, 2)

I know I could do something similar with lists by doing:

>>> a = [1]
>>> b = [2]
>>> c = (a, b)
>>> c
([1], [2])
>>> a[0] = 3
>>> c
([3], [2])

but this can be easily lost if one assigns a or b to something instead of their elements.

Is there a simple way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

溺深海 2024-10-03 10:00:24

不,Python 中没有直接的方法可以做到这一点。原因是标量值(数字)和元组都是不可变的。一旦建立了从名称到不可变值的绑定(例如名称 c 与元组 (1, 2)),除了重新分配 c 可以更改其绑定的值。

请注意,在第二个示例中,虽然元组本身是不可变的,但它包含对可变值的引用。因此,看起来好像元组发生了变化,但元组的身份保持不变,只有可变部分发生了变化。

No, there isn't a direct way to do this in Python. The reason is that both scalar values (numbers) and tuples are immutable. Once you have established a binding from a name to an immutable value (such as the name c with the tuple (1, 2)), nothing you do except reassigning c can change the value it's bound to.

Note that in your second example, although the tuple is itself immutable, it contains references to mutable values. So it appears as though the tuple changes, but the identity of the tuple remains constant and only the mutable parts are changing.

空心空情空意 2024-10-03 10:00:24

无论您想出倒数第二行的任何可能解决方案,都将始终破坏它:

a = 3

这将为变量分配全新的内容。除非 a 代表对象或某物的属性(或列表中的键,就像您在自己的示例中所做的那样),否则您将无法在第一个和最后一个之间建立关系一个

Whatever possible solution you come up with the second last line will always destroy it:

a = 3

This will assign a completely new content to the variable. Unless a stands for a property of an object or something (or a key in a list, as you did in your own example), you won't be able to have a relation between the first and last a.

怀里藏娇 2024-10-03 10:00:24

如果您只需要动态地将当前值放入元组中,则可以使用 lambda。您必须调用 c,而不仅仅是返回它或使用它,但这在您的情况下可能是可以接受的。像这样的事情:

>>> a = 1
>>> b = 2
>>> c = lambda: (a, b)
>>> c()
(1, 2)
>>> a = 3
>>> c()
(3, 2)

If you just need the current values to be placed in a tuple on the fly you could use a lambda. You'll have to call c, not just return it or use it, but that may be acceptable in your situation. Something like this:

>>> a = 1
>>> b = 2
>>> c = lambda: (a, b)
>>> c()
(1, 2)
>>> a = 3
>>> c()
(3, 2)
半城柳色半声笛 2024-10-03 10:00:24

Python 中没有办法,不仅因为数字是不可变的,还因为没有指针。将值包装在列表中会模拟您拥有指针,因此这是您能做的最好的事情。

There isn't a way in Python, not only because numbers are immutable, but also because you don't have pointers. Wrapping the value in a list simulates that you have pointers, so that's the best you can do.

烟雨凡馨 2024-10-03 10:00:24
class ByRefValue(object):
    def __init__(self, value):
        self.value = value

将其传递到您喜欢的任何地方,请记住您需要访问 value 成员而不是整个对象。


或者,如果 a 位于全局命名空间中,则 globals().get('a', 0) 将返回该值(如果不在全局命名空间中,则返回零) 。


最后:

import threading
tls = threading.local()

tls.a = 1

如果将 tls 导入到需要的每个模块中,您将在每个线程上访问相同的 a 值。根据您的程序的设置方式,这可能是可以接受的、理想的或无用的。

class ByRefValue(object):
    def __init__(self, value):
        self.value = value

Pass it around wherever you like, remembering that you need to access the value member rather than the entire object.


Alternatively, globals().get('a', 0) will return the value of a if it is in the global namespace (or zero if it isn't).


Finally:

import threading
tls = threading.local()

tls.a = 1

If you import tls into every module where you need it, you will access the same value for a on each thread. Depending on how your program is set up, this may be acceptable, ideal or useless.

孤独患者 2024-10-03 10:00:24

您可以尝试创建自己的指针类和自己的指针存储对象来模拟系统的内部堆栈。

You can try creating your own pointer class and your own pointer storage object to emulate the system's internal stack.

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