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

> As you can see, it is pass by reference.

No, it's passing a reference, but it is still not pass by reference, this is actually worth distinguishing because they are different behaviors. That function (by moonchild) does exactly what moonchild meant it to do, which was to demonstrate that Python is not pass by reference. Another function demonstrating that Python is not pass by reference:

  def swap(a, b):
    a, b = b, a

  a = [1,2]
  b = [3,4]
  print(a,b)
  swap(a,b)
  print(a,b)
If Python were pass by reference (versus passing a reference in these cases) then it would perform a swap, but it does not perform a swap because it is not pass by reference. C++ has pass by reference (as an option), as do Pascal and Ada, and more languages. But Python is not among them. An actual pass-by-reference swap, in C++:

  template<typename T> // template to be the most equivalent to the intended Python code
  void swap(T& a, T& b) {
    T t = a;
    a = b;
    b = a;
  }
That will actually perform a swap when called with `swap(x,y)`. Any language that offers (by option or by default) pass by reference can have an equivalent swap function, including a generic one like the above if the language offers some notion of generics or is dynamically typed (like Python).


Yes, pointer’s are themselves passed by value (no pointers to pointers) due to garbage collection where the ability to directly alter them would cause crashes (like basically every C++ program does if you spend enough time fuzzing it).

That doesn’t change the fact that they are indeed pointers (aka references).

How are pointers to pointers passed? You don’t want to reference another stack frame directly, so you pass by value.


>>> As you can see, it is pass by reference.

> Yes, pointer’s are themselves passed by value (no pointers to pointers) due to garbage collection where the ability to directly alter them would cause crashes (like basically every C++ program does if you spend enough time fuzzing it).

I'm going to be frank, you're arguing both sides here which is very frustrating. You've said that Python is pass-by-reference, that it passes references, and now that it is pass-by-value (I think that's what you're trying to say). So do you believe that Python is pass-by-reference or not? If you believe it is, and if mine and moonchild's functions demonstrating the opposite aren't persuasive, here's the Python FAQ itself saying that it is not, in fact, pass-by-reference:

https://docs.python.org/3/faq/programming.html#how-do-i-writ...

> Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there’s no alias between an argument name in the caller and callee, and so no call-by-reference per se.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: