I read the link to the question in the comments titled "How do I pass a variable by reference?" and some documentation. I believe normal Python assignments and parameter passing are technically passed by value. However, every assignment in Python automatically triggers the creation of a reference to an object. When you assigned a
to b
and passed a
as a parameter to your function, both lines of code triggered the creation of references to the object a
. The references were then passed as values to your function and to b
.
Unfortunately, I do not know any static analysis tool to warn you of these rules. I admit they are confusing, but the only solution I know is to read the documentation and try to test concepts using simple code snippets. Your code is a good example from which you could deduce Python's "rules" and potential problems.
A section in Python documentation states:
"...arguments are passed by assignment in Python. Since assignment just creates references to objects..."
Further, according to an answer to "How do I pass a variable by reference?",
If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object.
In your code, l += e
looks like a "rebinding" within a method and should not have modified a
. However, it can be inferred that Python treated += [e]
similar to .append(e)
.
It was stated that "rebinding" a function variable to which an out-of-scope parameter was passed should not affect the passed object; despite that, a
was modified by l += [e]
. Thus it can be inferred that Python did not treat += []
as a rebinding. I encourage others to add links thru the comments as well, in case of new information or corrections.
NOTE:
It can be helpful to use the Python id()
function on a variable. It shows the integer that Python assigns to identify an object throughout the object's "lifetime". Unfortunately, I do not know the exact definition of "lifetime". You are better off asking others or reading the documentation: Python id() built-in function