Argument passing is reference binding

So we've seen that it's quite possible to modify the objects through function argument references, but also that it's possible to rebind the argument references to new values. If you want to change the contents of a list parameter and have the changes seen outside the function, you could modify the contents of the list like this:

>>> def replace_contents(g):
... g[0] = 17
... g[1] = 28
... g[2] = 45
... print("g =", g)
...
>>> f
[14, 23, 37]
>>> replace_contents(f)
g = [17, 28, 45]

And indeed, if you check the contents of f you'll see that they have been modified:

>>> f
[17, 28, 45]

Function arguments are transferred by what is called "pass by object reference". This means that the value of the reference is copied into the function argument, not the value of the referred to object; no objects are copied.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset