Reputation: 143
Is it possible to create a pointer in Python to make a variable equal to a string after that string is changed elsewhere? Something like this below concept:
a = 'hello'
b = a
a = 'bye'
print(b) # is it possible have b be 'bye' without simply doing b = a again?
Upvotes: 1
Views: 5276
Reputation: 6922
As pointed out in the comments (and the link it points to), you initially have a
and b
both pointing to the string 'hello'
, but re-assigning a
does not affect b
, which will still point to the string 'hello'
.
One way to achieve what you want is to use a more complex object as a container for the string, e.g. a dictionary:
a = {'text': 'hello'}
b = a
a['text'] = 'bye'
print(b['text']) # prints 'bye'
Upvotes: 4