You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-01-29.14:15:53.381>created_at=<Date2018-01-29.13:57:45.806>labels= ['type-bug', 'invalid']
title='Modifying a list/dict effects all variables sharing that address'updated_at=<Date2018-01-29.14:15:53.359>user='https://github.com/64andy'
If multiple lists/dictionaries are in the same memory address (usually caused by var1 = var2), then altering one will effect every variable in that address.
The ways I've found to achieve this are:
>>> my_list[2] ="Spam"
>>> my_list +="9"
>>> my_list.insert(4, "Hello")
>>> dictvar.update({"Three": "Four"})
This was achieved using Python 3.6.4 32-bit and 3.6.3 64-bit (CPython), and happened in both IDLE and python.exe
List Example code:
x = ['a','b','c']
y = x #Now y and x share a memory address, because CPython does that
print(f"Sanity test - x and y share the same address = {x is y}")
y[1] = '123'
y += ["Foo"]
y.insert(-1, "Eleven")
#x's Expected Value: ['a','b','c']print(x) #Actual Value
Yep, that's the way Python works. You are modifying the same object through different names. Remember that in Python it is the objects that matter (which are identified in CPython via their memory address, but that's an implementation detail) and names are just handy references to those objects. There can be any number of names that reference a single object.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: