An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:
For those with:
-
ancient browsers that won’t run that memory graph web debugger
-
the attention span of a pigeon and can’t be bothered to read a long explanation that doesn’t address this particular pycon
>>> import copy >>> >>> a = [[0]] >>> c1 = a >>> c2 = a[:] >>> c3 = list(a) >>> c4 = a.copy() >>> c5 = copy.copy(a) >>> c6 = copy.deepcopy(a) >>> >>> c1[0].append(1) >>> c2[0].append(2) >>> c3[0].append(3) >>> c4[0].append(4) >>> c5[0].append(5) >>> c6[0].append(6) >>> >>> print(a) [[0, 1, 2, 3, 4, 5]]copy.deepcopymakes a separate memory copy intoc6. So appending toc6affectsc6, but notaThis is what i get when click Play button
Traceback (most recent call last): File "/lib/python3.12/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) File "/lib/python3.12/site-packages/micropip/transaction.py", line 55, in gather_requirements await asyncio.gather(*requirement_promises) File "/lib/python3.12/site-packages/micropip/transaction.py", line 62, in add_requirement return await self.add_requirement_inner(Requirement(req)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/micropip/transaction.py", line 151, in add_requirement_inner await self._add_requirement_from_package_index(req) File "/lib/python3.12/site-packages/micropip/transaction.py", line 186, in _add_requirement_from_package_index metadata = await package_index.query_package( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/micropip/package_index.py", line 286, in query_package raise ValueError( ValueError: Can't fetch metadata for 'memory-graph'. Please make sure you have entered a correct package name and correctly specified index_urls (if you changed them). Traceback (most recent call last): File "/lib/python3.12/site-packages/micropip/_commands/install.py", line 142, in install await transaction.gather_requirements(requirements) File "/lib/python3.12/site-packages/micropip/transaction.py", line 55, in gather_requirements await asyncio.gather(*requirement_promises) File "/lib/python3.12/site-packages/micropip/transaction.py", line 62, in add_requirement return await self.add_requirement_inner(Requirement(req)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/micropip/transaction.py", line 151, in add_requirement_inner await self._add_requirement_from_package_index(req) File "/lib/python3.12/site-packages/micropip/transaction.py", line 186, in _add_requirement_from_package_index metadata = await package_index.query_package( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/micropip/package_index.py", line 286, in query_package raise ValueError( ValueError: Can't fetch metadata for 'memory-graph'. Please make sure you have entered a correct package name and correctly specified index_urls (if you changed them).From console
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015. (Reason: CORS request did not succeed). Status code: (null). None of the “sha512” hashes in the integrity attribute match the content of the subresource. The computed hash is “z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==”. memory-graph.com Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015. (Reason: CORS request did not succeed). Status code: (null). None of the “sha512” hashes in the integrity attribute match the content of the subresource. The computed hash is “z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==”. memory-graph.com Content-Security-Policy: The report URI (about:blank) should be an HTTP or HTTPS URI. memory-graph.com Content-Security-Policy: (Report-Only policy) The page’s settings would block a worker script (worker-src) at https://memory-graph.com/py_worker.js from being executed because it violates the following directive: “worker-src 'none'” memory-graph.com Loading micropip, packaging pyodide.asm.js:10:95312 Loaded micropip, packaging pyodide.asm.js:10:95608 PythonError: pyodide.asm.js:10:52088
-
Another reason why raw pointers are superior to object nonsense
Isn’t “explicit is better than implicit” part of the Zen of Python?
Not everyone is a C or a Rust coder. In C, sending in a pointer, know it’s pass by reference, not pass by value.
In Python, depends if it’s immutable or mutable. Python coders eventually become hyper aware of the difference.
Python coders that want direct access to C libraries can use Cython CPython or ctypes. Probably missing a few options. Maybe others can chime in. This comes at the expense of much more complicated packaging.
Any package author that has underlying C code in their package automagically gains rock star status.


