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:

  • logging_strict@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    10 days ago

    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.deepcopy makes a separate memory copy into c6. So appending to c6 affects c6, but not a

    • logging_strict@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      10 days ago

      This 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