Hi people,
A simple question:
I have a dict:
>> a = { 'element': 'red' }
>> b = a.copy()
>> a, b
({ 'element': 'red' }, { 'element': 'red' })
>> b['element'] = 'green'
>>a, b
({ 'element': 'red' }, { 'element': 'green' })
It's ok, very well, but....
>>> a = { 'button': {'color': 'red'} }
>>> a
{'button': {'color': 'red'}}
>>> b = a.copy()
>>> a, b
({'button': {'color': 'red'}}, {'button': {'color': 'red'}})
>>> a['button']['color'] = 'green'
>>> a,b
({'button': {'color': 'green'}}, {'button': {'color': 'green'}})
Why color value green change ever on "a" dict??? It's a bug??
thx
···
--
/\/\ariano Di Felice
Java/PHP/Python WEB/GUI Developer
http://www.marianodifelice.it
mariano.difelice@gmail.com
Tel. 0735 703735
Cell +39 339 6407211
dict.copy() does a shallow copy of the dictionary. in the second case you end up with the same color dictionary.
read more about it here:
http://en.wikipedia.org/wiki/Object_copy
for deep copy use the copy module.
···
On Fri, May 30, 2008 at 9:11 PM, Mariano Di Felice mariano.difelice@gmail.com wrote:
Hi people,
A simple question:
I have a dict:
a = { ‘element’: ‘red’ }
b = a.copy()
a, b
({ ‘element’: ‘red’ }, { ‘element’: ‘red’ })
b[‘element’] = ‘green’
a, b
({ ‘element’: ‘red’ }, { ‘element’: ‘green’ })
It’s ok, very well, but…
a = { ‘button’: {‘color’: ‘red’} }
a
{‘button’: {‘color’: ‘red’}}
b = a.copy()
a, b
({‘button’: {‘color’: ‘red’}}, {‘button’: {‘color’: ‘red’}})
a[‘button’][‘color’] = ‘green’
a,b
({‘button’: {‘color’: ‘green’}}, {‘button’: {‘color’: ‘green’}})
Why color value green change ever on “a” dict??? It’s a bug??
thx
–
//\ariano Di Felice
Java/PHP/Python WEB/GUI Developer
http://www.marianodifelice.it
mariano.difelice@gmail.com
Tel. 0735 703735
Cell +39 339 6407211
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
–
There is NO FATE, we are the creators.