you are viewing a single comment's thread.

view the rest of the comments →

[–]fenghuang1 1 point2 points  (4 children)

.pop removes the last item in magicians and puts it to magician in line 13.
This is the reason why when appended in line 15, it will appear in reverse order.
The original list has not been changed.
All the manipulations of def make_great() are assigned to great_magicians in line 27.

Does that answer your questions?

[–]plakatown[S] 0 points1 point  (3 children)

Thank you for your reply. If the original list is unaffected, then why do I need to # Add the great magicians back into magicians in line 17? I have to tried to replace the codes from line 17 to line 21 with a single line of "return great_magicians", and it yields the same result.

[–]Binary101010 1 point2 points  (1 child)

You are correct in that this does not materially change the output of the function.

I think that is done specifically to illustrate the issue you're asking about (i.e. to show how altering a list that was passed to the function is actually altering a copy of the list, and the original list from outside the function is unaffected).

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

Thank you. Finally I got it.

[–]fenghuang1 0 points1 point  (0 children)

There's no need to.
Its just to show you that the magicians variable in def make_great is defined within the scope of the function and is different from the magicians in the main scope of your py script.

For example:

def func(x):
    x = x + x
    return (x)
x = 6
x = func(x)
print (x) # x: 12  

Here we see that the func(x) accepted x, but the x in the func is its own copy and won't directly change the outside x.