Python Forum
Pass by reference vs Pass by value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Pass by reference vs Pass by value (/thread-22614.html)



Pass by reference vs Pass by value - leodavinci1990 - Nov-19-2019

#What is the output?
val=100
print(val)
def changeVal():
    val=99
    print(val)
changeVal()
print(val)


#What is the output?
vals=[100,100,100]
print(vals)
def changeVals():
    vals[0]=99
    print(vals)
changeVals()
print(vals)
So I understand this as an example of pass by reference and pass by value.
The output is:

100
99
100
[100, 100, 100]
[99, 100, 100]
[99, 100, 100] #last line

1) Why isn't the last line [100,100,100]?
2) This is not a scope thing. Correct?


RE: Pass by reference vs Pass by value - jefsummers - Nov-20-2019

Assignment never copies data.
There is a great youtube from PyCon 2015 that covers names and values.
Ned Batchelder PyCon 2015