일반적인 두 변수 swap 하기

>>> a = 3
>>> b = 4
>>> a,b = b,a
>>> print(a, b)
4 3

서로 다른 두 리스트 swap 하기

>>> li_x = [1,2,3]
>>> li_y = [4,5,6]
>>> li_x, li_y = li_y, li_x
>>> print(li_x)
[4, 5, 6]
>>> print(li_y)
[1, 2, 3]

서로 다른 두 튜플 swap 하기

>>> tuple_x = (1,2,3)
>>> tuple_y = (4,5,6)
>>> tuple_x, tuple_y = tuple_y, tuple_x
>>> print(tuple_x)
(4, 5, 6)
>>> print(tuple_y)
(1, 2, 3)

Memory Address

>>> print(id(tuple_x))
140333848225728
>>> print(id(tuple_y))
140333847575680
>>> tuple_y, tuple_x = tuple_x, tuple_y
>>> print(id(tuple_x))
140333847575680
>>> print(id(tuple_y))
140333848225728

swap을 진행하면 두 변수들의 주소가 서로의 주소로 바뀌는 것을 확인할 수 있습니다.

원래 tuple_x 의 주소 : 140333848225728 → 바뀐 tuple_x의 주소 : 140333847575680