Python 中的指针? ` x.pointerDest = y.pointerDest`?

2024-02-17

我正在把我的老问题分成几个部分,因为它非常混乱here https://stackoverflow.com/questions/4590407/how-can-i-evaluate-variable-to-another-variable-before-assigning。这个问题与此相关answer https://stackoverflow.com/questions/4590407/how-can-i-evaluate-variable-to-another-variable-before-assigning/4590450#4590450和这个answer https://stackoverflow.com/questions/4590407/how-can-i-evaluate-variable-to-another-variable-before-assigning/4590456#4590456。我尝试理解指针,甚至不确定它们是否存在于 Python 中。

# Won't change x with y=4
>>> x = 0; y = x; y = 4; x
0

# Won't change y
>>> x = 0; y = x; x = 2; y
0

#so how can I change pointers? Do they even exist in Python?
x = 0
y.pointerDestination = x.pointerDestination   #How? By which command?
x = 2
# y should be 0, how?

[更新2:已解决]

也许,关于存在的矛盾陈述There are no pointers in Python. and Python does not have the concept of a "pointer" to a simple scalar value.。最后一个是否推断存在指向其他内容的指针,从而使第一个语句无效?


Python 中的标量对象是不可变的。如果您使用非标量对象(例如列表),您可以执行以下操作:

>>> x = [0]
>>> y = x
>>> y[0] = 4
>>> y
[4]
>>> x
[4]
>>> x is y
True

Python 没有指向简单标量值的“指针”的概念。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 中的指针? ` x.pointerDest = y.pointerDest`? 的相关文章

随机推荐