0
x,y = 30,40

def swap(x,y):
    x = x+y
    y = x-y
    x = x-y   
    print(x,y) #Here it swaps but as soon as function ends they go back to their previous values
    
swap(x,y)
print(x,y)  #still prints x as 30 and y as 40

In C/C++ we used & operator to pass their addresses to make this change, how can we do the same thing in python considering python follows pass by object reference model?

SKumar
  • 43
  • 7
  • That doesn't work with ```int```s, see [this question](https://stackoverflow.com/questions/15148496/passing-an-integer-by-reference-in-python). – BernieD Sep 18 '22 at 20:08
  • I see, so I have to wrap it in something like a list to make this kind of thing work. – SKumar Sep 18 '22 at 20:15

0 Answers0