3 ms·
You are wrong. You can modify the original list in your example by having foo() call my_list.append("pwnd!"), for example. You can even use id() to verify that
by yason 17y ago
You are wrong.
You can modify the original list in your example by having foo() call my_list.append("pwnd!"), for example. You can even use id() to verify that it really is the exactly same object that is visible to the caller and the callee.
Python doesn't pass by reference (in the aliasing variables C++ sense) and it doesn't pass by value (in the functional sense) but rather, it passes references.
Everything is an object anyway but objects aren't copied around. They're just bound or rebound to new variables.
In the above example Python creates new local variables for all function arguments and binds them to the objects passed in as arguments.
If those local variables are assigned to, they're just rebound to point to the new local object while the original variables in the caller code still remain bound to the original object.
- pwmanagerdied 17y agoI acknowledge that everything in your post is true (save myself being wrong ;) ) and was not arguing otherwise. I know that you can mutate mutable arguments passed to functions in Python, I was just saying that it isn't actually pass-by-reference as the individual I was replying to was saying. I've edited my post to clarify. :)
- andrewcooke 17y agoyou understand python fine; you haven't understood the article. read the article again - it's interesting, self consistent, and disagrees with the comp.lang.python consensus.
- deleted 17y ago[deleted]