3 ms·
The point is that you can't pass an object. The object lives on the heap and you always pass references around. Thats why when we pass in a list we can change t
by driax 16y ago
The point is that you can't pass an object. The object lives on the heap and you always pass references around. Thats why when we pass in a list we can change the contents. The passed-in value is a copy of the reference to our list object.
One of the trouble in python is if you really want to pass a reference to a true value such as an int. Usually I just wrap the int value in a list object because the syntax is fairly concise. Of course if you can you should avoid it, and in python 3 the most common occurrence is fixed with the nonlocal statement (which allows you to access the value of an outer scope, required with closures and non-reference values).