如何在 Python 中按名称引用整数
我想要一个引用,它读作“名称‘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])
但是如果分配,这很容易丢失a 或 b 到某物而不是其元素。
有没有一种简单的方法可以做到这一点?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,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 reassigningc
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.
无论您想出倒数第二行的任何可能解决方案,都将始终破坏它:
这将为变量分配全新的内容。除非
a
代表对象或某物的属性(或列表中的键,就像您在自己的示例中所做的那样),否则您将无法在第一个和最后一个之间建立关系一个
。Whatever possible solution you come up with the second last line will always destroy it:
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 lasta
.如果您只需要动态地将当前值放入元组中,则可以使用 lambda。您必须调用 c,而不仅仅是返回它或使用它,但这在您的情况下可能是可以接受的。像这样的事情:
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:
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.
将其传递到您喜欢的任何地方,请记住您需要访问
value
成员而不是整个对象。或者,如果
a
位于全局命名空间中,则globals().get('a', 0)
将返回该值(如果不在全局命名空间中,则返回零) 。最后:
如果将
tls
导入到需要的每个模块中,您将在每个线程上访问相同的a
值。根据您的程序的设置方式,这可能是可以接受的、理想的或无用的。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 ofa
if it is in the global namespace (or zero if it isn't).Finally:
If you import
tls
into every module where you need it, you will access the same value fora
on each thread. Depending on how your program is set up, this may be acceptable, ideal or useless.您可以尝试创建自己的指针类和自己的指针存储对象来模拟系统的内部堆栈。
You can try creating your own pointer class and your own pointer storage object to emulate the system's internal stack.