To clarify how Python handles two equal objects
Ethan Furman
ethan at stoneleaf.us
Tue Jan 10 18:29:23 EST 2023
On 1/10/23 12:03, Jen Kris via Python-list wrote:
> I am writing a spot speedup in assembly language for a short but computation-intensive Python
> loop, and I discovered something about Python array handling that I would like to clarify.
> But on the next iteration we assign arr1 to something else:
>
> arr1 = [ 10, 11, 12 ]
> idc = id(arr1) – 140260325308160
> idd = id(mx1[2]) – 140260325306880
>
> Now arr1 is no longer equal to mx1[2]...
If you want to have `arr1` to still be `mx1[2]` (and consequently for `mx1[2]` to now be `[10, 11, 12]` you need to
mutate `arr1` instead of reassigning it:
arr1[:] = [10, 11, 12]
--
~Ethan~
More information about the Python-list
mailing list