1

So, i have a simple function that just take a value and increment it. But it seems that python create new value instead of updating the reference value y.

def increment(x):
    x+=1
    
y = 0
increment(y)
print(y) # i thought I would have 1 but got 0

while this works just fine?

def add_new(x):
    x.append("new")
    
y = ['old']
add_new(y)
print(y) # ['old', 'new']

ali al-karaawi
  • 782
  • 4
  • 18

0 Answers0