python / cpython

The Python programming language
https://www.python.org/
Other
60.06k stars 29.08k forks source link

dictionary values returns pointers when the value is a list #33402

Closed 326e03e2-9ed7-48a8-9d33-9955e1fff547 closed 23 years ago

326e03e2-9ed7-48a8-9d33-9955e1fff547 commented 23 years ago
BPO 219450

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: ```python assignee = None closed_at = created_at = labels = [] title = 'dictionary values returns pointers when the value is a list' updated_at = user = 'https://bugs.python.org/jcastano' ``` bugs.python.org fields: ```python activity = actor = 'jhylton' assignee = 'jhylton' closed = True closed_date = None closer = None components = ['None'] creation = creator = 'jcastano' dependencies = [] files = [] hgrepos = [] issue_num = 219450 keywords = [] message_count = 2.0 messages = ['2180', '2181'] nosy_count = 2.0 nosy_names = ['jhylton', 'jcastano'] pr_nums = [] priority = 'normal' resolution = 'wont fix' stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue219450' versions = [] ```

326e03e2-9ed7-48a8-9d33-9955e1fff547 commented 23 years ago
>>> dictio = {}
>>> dictio['a'] = ['a',[],""]
>>> temp = dictio['a']
>>> temp
['a', [], '']
>>> temp[-1] = 'c'
>>> temp
['a', [], 'c']
>>> dictio['a']
['a', [], 'c']

This is not documented. A simple way to fix it, is to copy the list when the value is returned:

>>> dictio['a']
['a', [], 'd']
>>> temp = []+dictio['a']
>>> temp
['a', [], 'd']
>>> temp[-1] = 'e'
>>> dictio['a']
['a', [], 'd']
03bde425-37ce-4291-88bd-d6cecc46a30e commented 23 years ago

Python's reference semantics are documented. This is not a bug. If you want to copy a list when you extract it from a dictionary, then do it in your code.