Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying a list/dict effects all variables sharing that address #76893

Closed
64andy mannequin opened this issue Jan 29, 2018 · 2 comments
Closed

Modifying a list/dict effects all variables sharing that address #76893

64andy mannequin opened this issue Jan 29, 2018 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@64andy
Copy link
Mannequin

64andy mannequin commented Jan 29, 2018

BPO 32712
Nosy @bitdancer, @64andy

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 = None
closed_at = <Date 2018-01-29.14:15:53.381>
created_at = <Date 2018-01-29.13:57:45.806>
labels = ['type-bug', 'invalid']
title = 'Modifying a list/dict effects all variables sharing that address'
updated_at = <Date 2018-01-29.14:15:53.359>
user = 'https://github.com/64andy'

bugs.python.org fields:

activity = <Date 2018-01-29.14:15:53.359>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2018-01-29.14:15:53.381>
closer = 'r.david.murray'
components = []
creation = <Date 2018-01-29.13:57:45.806>
creator = '64andy'
dependencies = []
files = []
hgrepos = []
issue_num = 32712
keywords = []
message_count = 2.0
messages = ['311135', '311140']
nosy_count = 2.0
nosy_names = ['r.david.murray', '64andy']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32712'
versions = ['Python 3.6']

@64andy
Copy link
Mannequin Author

64andy mannequin commented Jan 29, 2018

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

@64andy 64andy mannequin added the type-bug An unexpected behavior, bug, or error label Jan 29, 2018
@bitdancer
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant