first-time questioner here so do highlight my mistakes.
I was grinding some Leetcode and came across a behavior (not related to the problem) in Python I couldn't quite figure out nor google-out. It's especially difficult because I'm not sure if my lack of understanding is in:
- recursion
- the
+=
operator in Python or variable assignment in general - or Python's pass-by-sharing behavior
- or just something else entirely
Here's the simplified code:
class Holder:
def __init__(self, val=0):
self.val = val
class Solution:
def runThis(self):
holder = Holder()
self.diveDeeper(holder, 5)
return
def diveDeeper(self, holder, n):
if n==0:
return 1
# 1) Doesn't result in mutation
holder.val += self.diveDeeper(holder, n-1)
# 2) Also doesn't result in mutation
# holder.val = holder.val + self.diveDeeper(holder, n-1)
# 3) !! Results in mutations
# returnVal = self.diveDeeper(holder, n-1)
# holder.val += returnVal
print(holder.val)
return 1
a = Solution()
a.runThis()
So yeah my main source of confusion is how (1) and (3) look semantically identical to me but results in two completely different outcomes:
================ RESTART: Case 1 ===============
1
1
1
1
1
>>>
================ RESTART: Case 3 ===============
1
2
3
4
5
>>>
From (2), it doesn't seem related to the +=
operator and for brevity, I haven't included the tens of variations I've tried but none of them have given me any leads so far. Would really appreciate any pointers in the right direction (especially in case I get blindsided in job interviews lmao)
PS: In case this is relevant, I'm using Python 3.8.2