2

everybody, recently I 've read some articles about [passing value in Python].

here's the link: How do I pass a variable by reference?

However, I'm confusing if I can understand it this way?

enter image description here

As you see:

  1. Outside the method(): the_link refs to the origin list obj

  2. Inside the method():

    • copy a same new refs the_link(_copy) to the origin list obj
    • if there is a change_refs statement.
    • then Python creates a new list obj inside the method.
    • finally, it let the_list(_copy) refs to this new list obj.
  3. back to Outside: the_link still refs to the origin list obj

if I consider it this way. The OUTPUT result is also the same.

But I'm not sure if this is OK or NOT?

Sorry that If I'm not expressing it correctly. Hope you guys understand = =

Community
  • 1
  • 1
  • Create a practical example in code, and print the id (`print(id(the_list))`) every step along the way to see what's going on. –  Jun 03 '16 at 05:03
  • Upvote for the content of your list ;) Try `the_list.append(['you', 'other', 'brothers', 'can\'t', 'deny'])`; this should fix your problem. – Will Jun 03 '16 at 05:09
  • Is there a code example that comes with that diagram / steps? It would really help clarify what you are not understanding. – Tadhg McDonald-Jensen Jun 03 '16 at 05:20

2 Answers2

2

You got it correct. In Python, parameters are passed by what's called reference value, which means a copy or value of the reference (or pointer if you come from a C background) is passed to the method. Think of these references/pointers as integers containing indices to locations in memory.

Since the "copy" pointer (this is how we'll refer to it from now on) is pointing to the object that the "original" pointer was pointing to, any modifications we make to the object referenced by the "copy" pointer will be reflected in the original object, because the object referenced by the "copy" pointer is the original object.

But, since we have two different references (integers) pointing to the same object in memory (original = XYZ location in memory and copy = XYZ location in memory), when the integral or location value itself of the copy is changed (that is another object is assigned to copy), the change isn't reflected in original because copy and original are two separate references/pointers.

If, however, copy was a pointer to the pointer of the object (ObjectType** copy in C), the change would have been reflected in the original pointer/reference too.

If we have the following program,

def mutate_list(copy):
    copy = [5, 6, 7, 8]
    print(original, copy)

original = [1, 2, 3, 4]
mutate_list(original)

The output will be [1, 2, 3, 4] [5, 6, 7, 8]

If we change the mutate_list method to the following:

def mutate_list(copy):
    copy.clear()
    copy.append([5, 6, 7, 8])
    print(original, copy)

The output would be [5, 6, 7, 8] [5, 6, 7, 8].

EvilTak
  • 7,091
  • 27
  • 36
-1

I think the concept you are missing is "variable scope". Variables created inside a function/method are in a different scope as variables outside the function/method. They are different even if they have the same name. You create a variable called the_list that references an object then inside the method call you create another variable called 'the_list' that references another object. they are two different variables in different scopes.

Try visiting http://pythontutor.com/ to help you visualize what's going on.

arachnivore
  • 147
  • 1
  • 7