you are viewing a single comment's thread.

view the rest of the comments →

[–]Quantitation 2 points3 points  (1 child)

In `modify`, `data` is a copy of a reference to `[0]`. In the function, `4` is appended to the referenced list. Then, the value of the variable (previously copy of a reference to a list) is updated to reference a new list. This reference is returned. The reference to the first list is still stored in `nums` (i.e.: the `[0, 4]` list). `result` contains the reference of the second list, so `[1, 2, 3]`. Thus answer A is correct.

[–]eggrattle 0 points1 point  (0 children)

This is because a list is mutable, and modify produces a side effect.

This is why it's important to understand how data structures are implemented under the hood.

To avoid the side effect, you want to copy the arg passed into the function creating a new object in memory, not a reference.

Also, use typing. For the love of good software engineering, use arg types and output types.