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

  • m532@lemmygrad.ml
    link
    fedilink
    arrow-up
    1
    ·
    10 days ago

    Let’s look at the suspects.

    b.append(), you add to the existing list. This mutates the current list. You can not be the culprit.

    b = b + [], you join up lists, making a new list in the process, that then gets stored in variable b, without changing the original list that’s still stored in variable a. You are not the culprit either.

    No, the culprit must be someone that ambiguously looks both like a mutation and an instantiation.

    Isn’t that right, b += []? Because the culprit… IS YOU!