Hacker News new | ask | show | jobs
by pizza234 1295 days ago
Passing a reference is semantically different from passing by reference (the latter being a functionality of the language); doing the former (behind the scenes) doesn't imply that the language supports the latter (if one wants to be rigorous, not even C supports pass by reference).

The canonical example is a function that swaps the variables passed to a function:

    a, b = 10, 20
    swap_fn(&a, &b)
    a == 20 # true
Since the above are primitive data types (ints), in order to make this work, the language needs to generically support passing the address (reference) of the variables. Java¹/Python² etc. are not able to do this; they copy the value of the variable and send it to the function, which will operate on the copy.

¹=at least, last time I've checked, which was long ago :)

²=funny to think that at least in Python, one can mess with the global register of the variables, and actually accomplish that

2 comments

I understand it is semantically different, but the interviewer should not be surprised you can change a variable in a function without returning it, which is the context of this thread. It's syntactic sugar for pointers.
In python it actually depends on the data type!

    Python 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def f(d):
    ...     d['foo'] = 'bar'
    ...
    >>> a = {}
    >>> f(a)
    >>> a
    {'foo': 'bar'}
That example actually holds with what the parent comment said. You aren't trying to modify d exactly, you're modifying d's contents.

  Python 3.8.10 (default, Jun 22 2022, 20:18:18) 
  [GCC 9.4.0] on linux
  Type "help", "copyright", "credits" or "license" for more 
  information.
  >>> def f(d):
  ...     d = {'foo': 'bar'}
  ... 
  >>> a = {}
  >>> f(a)
  >>> a
  {}
does not modify the passed in dictionary, because the reference 'd' itself is passed by value. So the function doesn't change what d references for the caller. Java works the same way.
My understanding of what's going on with python is it passes by reference (hence what your code does), but if the data type is immutable any modification results in a new instance being created in the scope of the function, and the object passed in is not modified. Which mostly looks like pass-by-value behaviour.

I guess my understanding could be technically wrong in some way, but it seems to reflect what happens.

The issue is that Python passes references by value, so you cannot overwrite what the arguments of a function reference from the perspective of the caller. However, if you pass something like a dictionary or a class the function gets a copy of the reference and can use it to update the members of what was passed. Java operates on the same principle, although there may be some edge cases I'm not aware of where the two differ.
Perhaps I'm being simple minded, but the id of the passed object within the function is the same as the id of the object outside the function, no? My mental model is that an object's id in python is basically an address / pointer. So, how come it's the same inside the function as outside, if it's not pass by reference?
You're correct that the id is the same inside and outside the function, but when you modify the passed in argument the id changes because you're setting the value of the parameter, not what it's referencing. In C it'd be like changing the value of a pointer in a function instead of the value referenced by a pointer in a function.

Here's an example to include the id differences and the fact that a does not take on the value of d:

  >>> def f(d):
  ...     print(f"id: '{id(d)}'")
  ...     d = {'foo': 'bar'}
  ...     print(f"id: '{id(d)}'")
  >>> a = {}
  >>> a
  {}
  >>> id(a)
  140362610196224
  >>> f(a)
  id: '140362610196224'
  id: '140362610196160'
  >>> a
  {}
  >>> id(a)
  140362610196224

But, as I've posted elsewhere in this thread, this example will change the contents of 'd' since d itself is not being set, its contents are:

  >>> def f(d):
  ...     d['foo'] 'bar'}
  ... 
  >>> a = {}
  >>> f(a)
  >>> a
  {'foo': 'bar'}
Until the second line of your function (in the first example), it's pass by reference, no? It stops behaving like pass-by-reference because you then reuse the name 'd' to create a new object, which has a different id and lives at a different address.

At least, this seems like a coherent mental model of what's happening, to me.