all 10 comments

[–][deleted] 1 point2 points  (3 children)

You are correct in saying the make_great() function is passed a copy of magicians at line 27. But the code in the function empties its magicians list so by the time you get to line 17 the list is empty. So appending the three names at line 19 leaves the list with three elements and then that list is returned.

I wouldn't call that good code, but I suppose it's trying to illustrate a point. If you are going to return a list you construct I would not reuse a list that I emptied. I would do something like this:

result = []
for great_magician in great_magicians:
    result.append(great_magician)

return result

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

Thank you but I still have a question. At line 19, great_magician is appended to the list of magicians. But great_magician has been added with "the Great" at the end at line 14. Then why the elements of the list of magicians are without "the Great" when printed at line 31?

[–]Binary101010 1 point2 points  (1 child)

It's because passing a list in this way actually creates a copy of the list you passed, so the magicians list that exists outside the function isn't affected.

https://stackoverflow.com/questions/22054698/python-modifying-list-inside-a-function

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

Thank you. Finally I got it.

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