是否可以在 Python 中创建一个指针,以便在该字符串在其他地方更改后使变量等于该字符串?像下面这样的概念:a = 'hello'b = aa = 'bye'print(b) # is it possible have b be 'bye' without simply doing b = a again?
1 回答

繁星点点滴滴
TA贡献1803条经验 获得超3个赞
正如评论中指出的(及其指向的链接),您最初拥有a和b都指向 string 'hello',但重新分配a不会影响b,它仍将指向 string 'hello'。
实现您想要的一种方法是使用更复杂的对象作为字符串的容器,例如字典:
a = {'text': 'hello'}
b = a
a['text'] = 'bye'
print(b['text']) # prints 'bye'
添加回答
举报
0/150
提交
取消