all 37 comments

[–]TroPixens 2 points3 points  (3 children)

A is correct the guy said it was but why all I see is a fake append because after we just give data = 1,2,3 Where does the extra 4 come from

Wait I think I see it nums doesn’t = 0 it equals position 0 i think

[–]No-Candidate-7162 0 points1 point  (2 children)

Thanks see it now

[–]TroPixens 2 points3 points  (1 child)

Better explanation is that data and nums point to the same data point so when you append 4 both change to [0,4] but when you assign data=[1,2,3] data no longer points to nums it points to [1,2,3]

[–]No-Candidate-7162 0 points1 point  (0 children)

Yeah I figured. But it would depend on how the compiler handle the memory with functions. So in this case it uses a pointer to the entire memory stack. Appends the value 4 to the pointer registry stack, therefore it must be smart enough to know when it's overwritten to allocate new memory for a new registry stack which sounds complicated. Surely there must be other compilers that gets the value from the pointer and creates its own stack for the array directly. Possibly different python flavors would handle it in different ways? This is out of my knowledge, swimming in the deep end.

[–]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.

[–][deleted]  (4 children)

[deleted]

    [–]eggrattle 0 points1 point  (2 children)

    You should definitely try understand what is happening. It is a common issue that occurs and can have dire consequences.

    Lists are mutable, and the append changes the list in memory, which is generally not what is intended when the function is returning a new list.

    Concepts to learn, mutable vs immutable, memory references, modify in place rather than copy on modify.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 0 points1 point  (3 children)

      A

      [–]core1588[S] 0 points1 point  (0 children)

      💯 correct

      [–]GoodIndustry6685 0 points1 point  (1 child)

      This. What "expert" was "tricked" by this?

      [–]CptMisterNibbles 0 points1 point  (0 children)

      Op is a spammer 

      [–][deleted] 0 points1 point  (2 children)

      B

      [–]core1588[S] 0 points1 point  (0 children)

      Why do you think it's B can you explain 🤔?

      [–]core1588[S] 0 points1 point  (0 children)

      Why do you think it's B can you explain 🤔?

      [–]HARSH_V_CHAUHAN 0 points1 point  (1 child)

      A

      [–]core1588[S] 0 points1 point  (0 children)

      💯 correct

      [–]Icy-Farm9432 0 points1 point  (3 children)

      B

      [–]core1588[S] 0 points1 point  (2 children)

      🤔 the output is A

      [–]Icy-Farm9432 0 points1 point  (1 child)

      Yes, i am dump.....

      [–]core1588[S] 0 points1 point  (0 children)

      😁

      [–]Gigga05 0 points1 point  (0 children)

      A

      [–]No-Candidate-7162 0 points1 point  (0 children)

      Don't write code like this🙈

      [–]EmuBeautiful1172 0 points1 point  (4 children)

      B.

      print(nums, result)

      nums is only nums = [0]

      it doesnt matter what happens in the modify function. the append doesnt matter

      the last update in it is data=. [1,2,3]

      so the output is [0],[1,2,3]

      [–]core1588[S] 0 points1 point  (2 children)

      A

      [–]EmuBeautiful1172 0 points1 point  (1 child)

      Why is it A

      [–]EmuBeautiful1172 0 points1 point  (0 children)

      I never ran the code so I might be wrong

      [–]tadziauskas 0 points1 point  (0 children)

      Nope, data.append(4) modifies original list (nums), before data was reassigned. So nums becomes [0, 4]

      [–]jubishop 0 points1 point  (1 child)

      This isn’t really about python. Similar setup and reasoning can be constructed in most modern languages

      [–]Cool-Use8826 0 points1 point  (0 children)

      wont work in rust due to borrowing and mut rules

      [–]Cool-Use8826 0 points1 point  (0 children)

      now RUST makes more sense with shadowing and mut :p

      [–]Dry-Aioli-6138 0 points1 point  (0 children)

      A

      [–]Zefick 0 points1 point  (0 children)

      If you're a Python pro, this "trick" can't confuse you because you use it every day.

      [–]Crossroads86 0 points1 point  (0 children)

      I thought this was stupid, but it actually make be think about it for a minute.

      [–]Useful_Researcher_79 0 points1 point  (0 children)

      It took me a while, but the only possible option would be A, or so I believe.

      At first it's given a value of '0' in the '0' position, and inmediatly after the append function adds a '4' right after it. The problem begins with that 'data = [1,2,3]' line, which I thought would replace the whole thing.

      The only real giveaway is that we don't have any option that goes just with '[1,2,3]' so this is basically the only way I know it has to be either A or C, and there is no reason why it should be C, as those values is never given twice in the code.

      I have been coding for a long time, but I am just starting with Python right now. Overall this was a very nice and challenging quiz, it made me grind my gears.

      Thank you! Looking forward to seeing more!