1

I have doubts about this Standford University paper (I recommend it, I found it on the Internet), but a question arose about this code:

# parameters are passed via binding
def main():
    original = [1, 2, 3]
    do_your_thing(original)
    print('original', original)

# when this function is called, the param_name in do_your_thing
# is "bound" to the same value as original.
def do_your_thing(param_name):

    # param_name = variable - is this understading correct?
    print(param_name)`
  1. Can I imagine that behind the scenes something like param_name = original happens?

  2. In case my understanding is correct, the same would happen if the original variable were an immutable object, i.e., for example 9800?

These 2 questions are just to try to understand the passing of parameters in functions, I don't want to do something concrete.

gus
  • 123
  • 5
  • Yes, that's exactly what happens. The value of `original` is bound to the name `param_name` as part of the process of calling `do_your_thing`. Mutability does not affect the assignment process at all. – chepner Apr 27 '23 at 20:12
  • You'll want to read https://nedbatchelder.com/text/names.html. – chepner Apr 27 '23 at 20:14
  • hi @chepner - Just these days I also read that article, but I want to confirm if I understood it (it's very confusing stuff). So, the same thing would happen behind the scenes if `original` was an immutable object (question 2), right? – gus Apr 27 '23 at 20:21
  • Yes. Another way of looking at it is that the type of an object really doesn't specify whether it is mutable or not: that's entirely defined by the methods available on the object, and *no* methods are involved/called/etc during assignment to a name. – chepner Apr 28 '23 at 12:06
  • Great, now it's clear to me @chepner – gus Apr 28 '23 at 12:40

1 Answers1

0

That's correct! Another way of saying it is that function arguments are always passed by reference, regardless of type (it's a bit more nuanced than that, but you can read more about that here). If a function mutates one of its arguments, that change will be visible outside of the function. Of course, in the case of immutable objects, this isn't an issue, so it doesn't really make a difference whether the object is passed by reference or by value.

For a much better explanation of the difference between passing arguments by reference vs. value than I could give, see the SO article here.

Caleb Keller
  • 553
  • 3
  • 19
  • hi @Caleb Keller - "*so it doesn't really make a difference whether the object is passed by reference or by value*" - "by reference" you mean a **mutable object** and "by value" an **immutable object**, or what do you mean by that? – gus Apr 27 '23 at 20:33
  • Passing an object by reference means that the variable names inside the function refer to the same object that was passed in from outside the function. Passing by value means that the object is copied, and the variable names inside the function don't refer to the objects on the outside, only their copies. The mutability of an object doesn't have any effect on whether it can be passed by reference or value - hopefully that makes sense! – Caleb Keller Apr 27 '23 at 20:37
  • I understand, I was just asking because passing by value and by reference are not common in python. So, the answer to my question number 2 is YES, that is, does a "normal" assignment **always** happen behind the scenes (in this case `param_name = variable`) regardless of whether it is mutable or immutable object? @Caleb Keller – gus Apr 27 '23 at 23:16
  • @gustavo Correct, passing by value never happens in Python, but arguments are *always* passed by reference (technically assignment, but those terms are nearly identical) regardless of whether the object being passed is mutable or immutable. – Caleb Keller Apr 28 '23 at 04:49
  • excellent, You gave another very *different approach* to this "mutation vs binding / rebinding" issue, but still helped me confirm my knowledge (my 2 questions), so I will mark it as the best answer @Caleb Keller – gus Apr 28 '23 at 12:49