0

I need help to understand the below;

def increment(n):
    n += 1
    return n

a = 1
increment(a)
print(a)

The output is 1 and not 2 as per my little understanding of Python.

I have added the line return n in an attempt to pass on the value from n to a. It still does not.

As per a comment from @TimRoberts below, editing the above code with a = increment(a) gives an output of 2.

I am writing this here to highlight this one of many good comments below.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Phoenix IV
  • 53
  • 4
  • 1
    This would be explained in any decent Python introduction. Hint: What is the *scope* of the variable `n`? – tadman Jan 04 '23 at 19:19
  • 1
    @tadman A better dupe target would be numbers vs lists. If similar code were written for lists (`n += [1]`), it'd behave differently. – iBug Jan 04 '23 at 19:20
  • @tadman does adding a return statement make sense as regards to scope – Phoenix IV Jan 04 '23 at 19:31
  • 2
    @iBug true, however scope is very important in general, but `+=` operator calls a mutator method underneath the hood. But understanding scope and assignments is important in general. Python never supports call by reference, which seems to be the semantics the OP is expecting. – juanpa.arrivillaga Jan 04 '23 at 19:31
  • 1
    Adding a return statement would work if you called it as `a = increment(a)`. – Tim Roberts Jan 04 '23 at 19:32
  • 2
    @PhoenixIV a `return` statement does not affect scope. It affects what the function call *evaluates* to. – juanpa.arrivillaga Jan 04 '23 at 19:32
  • 1
    If you want to get data out of that scope, `return` is a great way of doing it. If you don't do that, whatever happens in that scope, stays in that scope. – tadman Jan 04 '23 at 19:35
  • if I add a = increment(a) as per what @TimRoberts has said, a becomes 2. I never knew coding was this complex, respect and thanks guys. – Phoenix IV Jan 04 '23 at 19:37
  • 1
    It's not complicated. You just need to understand the structure of your language. In some languages, your code would have done what you expect. Python has some different rules. This particular rule is a GOOD one, because the caller shouldn't have to worry about whether the function is going to change `a` under the hood. – Tim Roberts Jan 04 '23 at 19:41
  • 1
    @juanpa.arrivillaga Might be better if clearer: Python always call by reference w.r.t. *objects*, but never affects *name binding*. In other words, the same object is always passed from the caller to the callee, but the callee knows nothing about which *name* in the caller's scope refers to. – iBug Jan 04 '23 at 19:46
  • @iBug no, because that is **absolutely incorrect**. Python is *never* call by reference. Call be reference would work like this: `def foo(&x): x = 0` then `y = 1; foo(y); print(y)` would print `0`. It is better to use terminology correctly, since call by reference means a specific thing, and Python does not support it. Fortran was the classic example. Indeed, I can't think of many modern languages that do. C++ (but not C) does. I believe C# does as well. – juanpa.arrivillaga Jan 04 '23 at 20:09
  • @iBug IOW, "but the callee knows nothing about which name in the caller's scope refers to." then by definition, **it is not call be reference**. Call by reference is essentially when the parameter in the caller *acts as an alias* for the variable passed as an argument. So any assignment to the parameter is seen in the caller. That is *what call by reference means*: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference – juanpa.arrivillaga Jan 04 '23 at 20:09
  • @iBug if my own rants don't convince you, read the [CLU Reference Manual](http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-225.pdf) by Barbara Liskov herself. In section 3.2 Assignment and Invocation this evaluation strategy is defined as "call by sharing" and distinguished from both call by value and call by reference. CLU is hugely influential. Python's semantics in this regard are the same (and borrows a lot from CLU). Not just Python though. Java, Javascript, Ruby (I believe), Julia... – juanpa.arrivillaga Jan 04 '23 at 20:19

0 Answers0