Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Actually in Python is pass-by-reference with lists and dictionaries.


This is a common point of confusion. There are two distinct orthogonal language properties at play here:

1) Variables being references(pointers) to objects.

2) Function arguments being passed by reference or by value.

In Python, all variables are pointers to heap allocated objects, and these pointers are simply passed by value when calling functions.

In C++, it is much easier to understand this distinction as it provides full control to the programmer on both aspects.


Lists and dictionary aren't special, and they aren't pass-by-reference in the sense referred to in the article.

    def foo(my_list):
      my_list = [ "my", "new", "list" ]
Executing this function will not change the value of the list passed to it, it will create a new list and reference it locally. If calling foo(old_list) caused old_list to now contain [ "my", "new", "list" ], then it would be pass-by-reference in the sense this article refers to.

EDIT: To clarify, you can mutate mutable arguments in-place, but you can't change what is being referred to by the identifiers used to refer to these arguments outside of the context of the function, which is what is meant by pass-by-reference in the article.


Though my_list.append("blub") does change the outside list. Does this matter?


In Python you never deal with an object itself, just a reference to it, so you can change the value of any mutable object you pass to a function, lists and dictionaries are just a couple of examples of these.

The problem (discussed by the article) is that "pass-by-reference" is a term that is used a couple of different ways. By someone's definition, this property of Python objects might cause them to consider the calls to pass-by-reference, but the way the author defined it in this article they are not.


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.


you 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.


I 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. :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: