An exercise to help build the right mental model for Python data.

The “Solution” link visualizes execution and reveals what’s actually happening using 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: https://github.com/bterwijn/memory_graph

  • tomenzgg@midwest.social
    link
    fedilink
    English
    arrow-up
    3
    ·
    1 day ago

    Eh, I get it. The equal operator creates a reference but the plus operator isn’t destructive so it creates a new list and overwrites the variable b with a new list, when assigned.

    Of course, this would all be avoided if creating copies was the norm; which is why I stick with functional languages.

    • bterwijn@programming.devOP
      link
      fedilink
      arrow-up
      2
      ·
      24 hours ago

      Copying a list with a million elements every time you make a small change is not fun. Sure, you can optimize a bit behind the scenes, but that still gives a lot of overhead.

      • tomenzgg@midwest.social
        link
        fedilink
        English
        arrow-up
        2
        ·
        20 hours ago

        And we can create data structures and algorithms that fit a more functional style without relying on imperative assumptions of how data should be handled. Data structures like vlists could be applicable, for example.