Python Forum

Full Version: Pass by reference vs Pass by value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#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?
Assignment never copies data.
There is a great youtube from PyCon 2015 that covers names and values.
Ned Batchelder PyCon 2015