Understanding Python memory management with dict -


this question has answer here:

when writing python codes using dict, found out following behavior of code:

in [1]: def foo(bar_dict):    ...:     print id(bar_dict)    ...:     bar_dict['new'] = 1    ...:     return bar_dict    ...:   in [2]: old_dict = {'old':0}  in [3]: id(old_dict) out[3]: 4338137920  in [4]: new_dict = foo(old_dict) 4338137920  in [5]: new_dict out[5]: {'new': 1, 'old': 0}  in [6]: id(new_dict) out[6]: 4338137920  in [7]: old_dict out[7]: {'new': 1, 'old': 0}  in [8]: id(old_dict) out[8]: 4338137920 

the old_dict, new_dict , bar_dict inside foo function point on memory address. there 1 dict object stored in memory, pass dict inside function.

i want know more detail kind of memory management mechanism of python, can points me references explain this? also, when use list, set or str in python, there similar behavior?

python names references objects stored on heap. handing objects function call passes in references, binding argument name same object.

you created dictionary object, , bound old_dict object. passed name foo() function, binding local name bar_dict same object. in function manipulated object, , returned it. stored reference returned object in new_dict, resulting in 2 global names referencing same object.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -