TIL18. Python : Copy & Deep Copy

ID์งฑ์žฌยท2021๋…„ 9์›” 28์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
26/39
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Python์˜ ๊ฐ์ฒด ์ฐธ์กฐ์˜ ์ดํ•ด๋ฅผ ํ†ตํ•ด ์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ์— ๋”ฐ๋ฅธ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ Copy & Deep Copy

๐Ÿ”ฅ ๊ฐ์ฒด์˜ ์ฐธ์กฐ์™€ ๋น„๊ต์—ฐ์‚ฐ์ž

๐Ÿ”ฅ ์–•์€๋ณต์‚ฌ(Copy) ๊นŠ์€๋ณต์‚ฌ(Deep Copy)๋ž€ ๋ฌด์—‡์ผ๊นŒ?

๐Ÿ”ฅ ๊ฐ€๋ณ€ํ˜•๊ณผ ๋ถˆ๋ณ€ํ˜•์˜ ๊ฐ์ฒด ์ฐธ์กฐ



1. ๊ฐ์ฒด์˜ ์ฐธ์กฐ์™€ ๋น„๊ต์—ฐ์‚ฐ์ž

๐Ÿค” "==" ๊ณผ "is" ์ฐจ์ด์ 

โœ”๏ธ x์— ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๊ณ  y์— x๋ฅผ ํ• ๋‹น์‹œํ‚ค๋ฉด, x์™€ y๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— ์„œ๋กœ ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , id๊ฐ’๋„ ์„œ๋กœ ๋™์ผํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ์ฆ‰, x,y๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ค‘ ํ•˜๋‚˜๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋ชจ๋‘ ์ˆ˜์ •๋ฉ๋‹ˆ๋‹ค.

x = {"name": "Jang", "age": 33, "city": "Seoul"} # ๐Ÿ‘ˆ x๋ผ๋Š” ๋ณ€์ˆ˜์— ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
y = x # ๐Ÿ‘ˆ y๋ผ๋Š” ๋ณ€์ˆ˜์— x๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ์ฐธ์กฐ๊ฐ’์„ ๊ฐ€๋ฅดํ‚ต๋‹ˆ๋‹ค.
print(x == y)  # True
print(x is y)  # True
print(id(x), id(y))  # 140643930504064 140643930504064
print(id(x) == id(y))  # True
x["sex"] = "Male"
print(x, y)  # {'name': 'Jang', 'age': 33, 'city': 'Seoul', 'sex': 'Male'} {'name': 'Jang', 'age': 33, 'city': 'Seoul', 'sex': 'Male'}

โœ”๏ธ ๊ฐ™์€ ๊ฐ’์„ x์™€ z์— ๊ฐ ๊ฐ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๊ณ  ๋น„๊ตํ•˜๋ฉด, ๊ฐ’์€ ๊ฐ™์ง€๋งŒ ๋‹ค๋ฅธ ๋…๋ฆฝ์  ๊ฐ์ฒด์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ๋™๋“ฑ์—ฐ์‚ฐ์ž(==)๋Š” ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , is๋Š” ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€ ๊ฐ์ฒด ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•˜๋Š”๋ฐ, ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•  ๋•Œ, is๊ฐ€ ==๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค.
โœ”๏ธ ๊ฐ์ฒด ์•ˆ์— ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋น„๊ตํ•  ํ•„์š”์—†์ด ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

x = {"name": "Jang", "age": 33, "city": "Seoul", "sex": "Male"}
z = {"name": "Jang", "age": 33, "city": "Seoul", "sex": "Male"}
print(x, z) # {'name': 'Jang', 'age': 33, 'city': 'Seoul', 'sex': 'Male'} {'name': 'Jang', 'age': 33, 'city': 'Seoul', 'sex': 'Male'}
print(x == z)  # True
print(x is z)  # False
print(id(x), id(z))  # 140260488281024 140260488281280


2. ์–•์€๋ณต์‚ฌ(Copy) ๊นŠ์€๋ณต์‚ฌ(Deep Copy)๋ž€ ๋ฌด์—‡์ผ๊นŒ?

๐Ÿค” ๋ณ€์ˆ˜์˜ ํ• ๋‹น๊ณผ ๋ณต์‚ฌ๋Š” ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ?

โœ”๏ธ copy๋Š” ์–•์€ ๋ณต์‚ฌ๋ผ ๋ถˆ๋ฆฌ๊ณ , ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค์†Œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ๊ฐ’์„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” list()์— ๋‹ด์œผ๋ฉด, ์ด๋ฅผ ์–•์€ ๋ณต์‚ฌ(copy)์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์€ ๊ฐ™์ง€๋งŒ ๋‹ค๋ฅธ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด True๊ฐ€ ๋‚˜์˜ค์ง€๋งŒ, is๋กœ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•˜๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

data1 = [10, [100, 105], (5, 10, 15)]
data2 = data1         # ๐Ÿ‘ˆ ํ• ๋‹น
data3 = list(data1)   # ๐Ÿ‘ˆ copy
print(data1 == data2) # True
print(data1 is data2) # True
print(data1 == data3) # True
print(data1 is data3) # False

โœ”๏ธ ๊ฐ์ฒด์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ฉด, data1, data2 ๊ฐ์ฒด์—์„œ๋งŒ ๊ฐ’์ด ์ถ”๊ฐ€๋˜๊ณ , ๊ฐ์ฒด ๋‚ด ์›์†Œ ์ค‘ ๋ฆฌ์ŠคํŠธ ๋‚ด์— ๊ฐ’์„ removeํ–ˆ๋”๋‹ˆ data1, data2, data3 ๋ชจ๋‘ ๊ฐ’์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์ด ์ฐจ์ด๋Š” ๊ฐ์ฒด data1, data2๋Š” ๊ฐ™์€ ๊ฐ์ฒด์ด๊ณ  data3๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์ง€๋งŒ, ๊ฐ์ฒด ๋‚ด ์›์†Œ๋“ค์ด ๋™์ผํ•œ id๊ฐ’์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
โœ”๏ธ ์ฆ‰, ์–•์€ ๋ณต์‚ฌ๋ฅผํ•˜๋ฉด ๋ณต์‚ฌํ•œ ๊ฐ์ฒด ์ž์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ๋‚˜์˜ค์ง€๋งŒ ๊ฐ์ฒด ๋‚ด ์š”์†Œ๋“ค์€ ํ• ๋‹นํ–ˆ์„ ๋•Œ ์ฒ˜๋Ÿผ ๊ฐ™์€ id ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

data1 = [10, [100, 105], (5, 10, 15)]
data2 = data1         # ๐Ÿ‘ˆ ํ• ๋‹น
data3 = list(data1)   # ๐Ÿ‘ˆ copy
print(data1 == data2) # True
print(data1 is data2) # True
print(data1 == data3) # True
print(data1 is data3) # False
# ์ˆ˜์ •
data1.append(1000)
data1[1].remove(105)
print(data1)  # [10, [100], (5, 10, 15), 1000]
print(data2)  # [10, [100], (5, 10, 15), 1000]
print(data3)  # [10, [100], (5, 10, 15)]

โœ”๏ธ ์ด๋ฒˆ์—๋Š” ๊ฐ์ฒด ๋‚ด ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ˆ˜์ •ํ•˜๊ธฐ ์ „ ๊ฐ์ฒด ๋‚ด ์š”์†Œ์˜ id๊ฐ’์„ ํ™•์ธํ•ด๋ณด๋ฉด ๋ชจ๋‘ ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ๊ฐ์ฒด ๋‚ด ๋ฆฌ์ŠคํŠธ์˜ ์ˆ˜์ •์„ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด, ๋ชจ๋‘ ๊ฐ™์€ id๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ, ํŠœํ”Œ์—์„œ๋Š” ๋‹ค๋ฅธ ์–‘์ƒ์„ ๋ณด์ž…๋‹ˆ๋‹ค.
โœ”๏ธ ํŠœํ”Œ์€ ๋ถˆ๋ณ€์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด์— data1[2]๊ณผ data2[2]์˜ id๊ฐ’์€ ํ•จ๊ป˜ ๋ณ€ํ–ˆ์ง€๋งŒ, data3[2]๋Š” id๊ฐ’์ด ๊ทธ๋Œ€๋กœ ์ž…๋‹ˆ๋‹ค.
โœ”๏ธ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์†Œ๋น„ํ•˜๊ณ , ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ์•ˆ์— ํŠœํ”Œ์„ ๋„ฃ๋Š” ๊ฒƒ์€ ์ถ”์ฒœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

print(id(data1[1]), id(data2[1]), id(data3[1])) # 140677041846656 140677041846656 140677041846656
print(id(data1[2]), id(data2[2]), id(data3[2])) # 140677041885632 140677041885632 140677041885632
data1[1] += [110, 120]
data1[2] += (110, 120)
print(data1)  # [10, [100, 110, 120], (5, 10, 15, 110, 120), 1000]
print(data2)  # [10, [100, 110, 120], (5, 10, 15, 110, 120), 1000]
print(data3)  # [10, [100, 110, 120], (5, 10, 15)]
print(id(data1[1]), id(data2[1]), id(data3[1])) # 140677041846656 140677041846656 140677041846656
print(id(data1[2]), id(data2[2]), id(data3[2])) # 140677003933712 140677003933712 140677041885632

๐Ÿค” ์–•์€ ๋ณต์‚ฌ(Copy)์™€ ๊นŠ์€ ๋ณต์‚ฌ(Deep Copy) ์ฐจ์ด

โœ”๏ธ ๊นŠ์€ ๋ณต์‚ฌ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ฟŒ๋ฆฌ๊นŒ์ง€ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๊ฐ ์š”์†Œ๊นŒ์ง€ ๋…๋ฆฝ๋œ id๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
โœ”๏ธ ์–•์€ ๋ณต์‚ฌ๋ฅผ ํ–ˆ์„ ๋•Œ ์–ด๋– ํ•œ ์ฐจ์ด๋ฅผ ๋ณด์ด๋Š”์ง€ ์•„๋ž˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ฝ”๋“œ์—์„œ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ basket2๋Š” basket1์„ ์–•์€ ๋ณต์‚ฌํ•˜์˜€๊ณ , basket3๋Š” basket1์„ ๊นŠ์€ ๋ณต์‚ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ์ด ๊ฒฐ๊ณผ, 3๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋Š” ๋ชจ๋‘ ๋‹ค๋ฅธ ๋‹ค๋ฅธ ๊ฐ์ฒด์ง€๋งŒ, ๊ฐ์ฒด ๋‚ด๋ถ€์˜ products๋ฅผ ์‚ดํŽด๋ณด๋ฉด, "basket1._products"๊ณผ "basket2._products"์ด ๊ฐ™์€ id๋ฅผ ๊ฐ–๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.

class Basket:
    def __init__(self, products=None):
        if products is None:
            self._products = []  # ๐Ÿ‘ˆ ์ƒํ’ˆ์ด ์—†์„ ๊ฒฝ์šฐ,
        else:
            self._products = list(products)  # ๐Ÿ‘ˆ ์ƒํ’ˆ์ด ์ด๋ฏธ ์žˆ๋Š”๋ฐ ์ถ”๊ฐ€๋  ๊ฒฝ์šฐ,
    # ์ƒํ’ˆ ์ถ”๊ฐ€
    def put_prod(self, prod_name):
        self._products.append(prod_name)
    # ์ƒํ’ˆ ์ œ๊ฑฐ
    def del_prod(self, prod_name):
        self._products.remove(prod_name)
basket1 = Basket(["Apple", "Bag", "TV", "Snack", "Water"])
basket2 = copy.copy(basket1)  # ๐Ÿ‘ˆ ์–•์€ ๋ณต์‚ฌ
basket3 = copy.deepcopy(basket1)  # ๐Ÿ‘ˆ ๊นŠ์€ ๋ณต์‚ฌ
print(id(basket1), id(basket2), id(basket3))  # 140659652628384 140659652627856 140659652627184
print(id(basket1._products), id(basket2._products), id(basket3._products))  # 140687808135040 140687808135040 140687808003776

โœ”๏ธ ์ด๋Š” ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ’์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— basket1์—์„œ ์ˆ˜์ •์ด ๋ฐœ์ƒํ•˜๋ฉด, basket2์˜ ์ƒํ’ˆ๋„ ์ˆ˜์ •๋˜๋Š” ์ผ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— ๋ณต์‚ฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ๋„ ๋…๋ฆฝ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๊ธธ ์›ํ•œ๋‹ค๋ฉด ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

basket1.put_prod("MacBook")
basket2.del_prod("Snack")
print(basket1._products) # ['Apple', 'Bag', 'TV', 'Water', 'MacBook']
print(basket2._products) # ['Apple', 'Bag', 'TV', 'Water', 'MacBook']
print(basket3._products) # ['Apple', 'Bag', 'TV', 'Snack', 'Water']


3. ๊ฐ€๋ณ€ํ˜•๊ณผ ๋ถˆ๋ณ€ํ˜•์˜ ๊ฐ์ฒด ์ฐธ์กฐ

๐Ÿค” ํ•จ์ˆ˜์˜ parameter๋กœ ์ „๋‹ฌ๋  ๋•Œ์˜ ๊ฐ์ฒด ์ฐธ์กฐ

โœ”๏ธ ์•„๋ž˜ plus ํ•จ์ˆ˜๋Š” x,y๋ฅผ parameter๋กœ ์ „๋‹ฌ ๋ฐ›์•„ ๊ทธ ๊ฐ’์„ ๋”ํ•œ ํ›„ x์— ํ• ๋‹นํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ parameter๋กœ ๊ฐ€๋ณ€ํ˜•๊ณผ ๋ถˆ๋ณ€ํ˜•์ด ์ „๋‹ฌ๋  ๋•Œ, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

def plus(x, y):
    x += y
    return x

โœ”๏ธ ๋ถˆ๋ณ€ํ˜• str๊ณผ tuple์€ ์›๋ณธ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, parameter๊ฐ€ ์ „๋‹ฌ๋˜๋‹ค๋Š” ๊ฒƒ์€ ์ฐธ์กฐ๊ฐ’์„ ์ „๋‹ฌ์‹œํ‚ค๋Š”๋ฐ ๋ถˆ๋ณ€ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด์— ํ•จ์ˆ˜ ๋ฐ–์—์„œ c์˜ id๊ฐ’๊ณผ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” c์˜ id๋Š” ๋‹ค๋ฅธ ๊ฐ’์ž…๋‹ˆ๋‹ค.

x = 4
y = 8
print(plus(x, y), x, y)  # 12 4 8
c = (10, 100)
d = (5, 10)
print(plus(c, d), c, d)  # (10, 100, 5, 10) (10, 100) (5, 10)

โœ”๏ธ ๊ฐ€๋ณ€ํ˜•์ผ ๋•Œ๋„ ์ „๋‹ฌ๋˜๋Š” parameter๋Š” ์ฐธ์กฐ๊ฐ’์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ฐ€๋ณ€ํ˜•์ผ ๋•Œ๋Š” ํ•จ์ˆ˜ ์™ธ๋ถ€์˜ a์™€ ๋ฐ˜ํ™˜๋˜๋Š” a๊ฐ€ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •๋„ ๋ฐœ์ƒ๋ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์ด์— ํ•จ์ˆ˜ ๋ฐ–์—์„œ a์˜ id๊ฐ’๊ณผ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” b์˜ id๋Š” ๊ฐ™์€ ๊ฐ’์ž…๋‹ˆ๋‹ค.

def plus(x, y):
    x += y
    return x
# ๊ฐ€๋ณ€ํ˜•์ผ ๋•Œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ์ฐธ์กฐ๊ฐ’์„ parameter๋กœ ์ „๋‹ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
# ์›๋ณธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด๋„ ๋  ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ
a = [10, 100]
b = [5, 10]
print(plus(a, b), a, b)  # [10, 100, 5, 10] [10, 100, 5, 10] [5, 10]

โœ”๏ธ ์ด์— ๋ฐ์ดํ„ฐ๋ฅผ parameter๋กœ ์ „๋‹ฌํ•  ๋•Œ, ์›๋ณธ์˜ ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜๋„ ๋ฌธ์ œ ์—†๋‹ค๋ฉด ๊ฐ€๋ณ€ํ˜•์ธ list๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์›๋ณธ์˜ ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜๋ฉด ์•ˆ๋  ๋•Œ๋Š” tuple์„ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๐Ÿค” ๋ถˆ๋ณ€ํ˜•์€ ๊ฐ์ฒด ๋ณต์‚ฌ๊ฐ€ ๋ ๊นŒ?

โœ”๏ธ ๋ถˆ๋ณ€ํ˜• ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค(str, bytes, frozenset, tuple...)์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์‚ฌ๋˜์ง€ ์•Š๊ณ  ๋ฌด์กฐ๊ฑด ์›๋ณธ์„ ์ฐธ์กฐํ•˜์—ฌ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ์–ด์ฐจํ”ผ ๋ถˆ๋ณ€ํ˜• ํƒ€์ž…์€ ๊ฐ’์˜ ์ˆ˜์ •์ด ๋ฐœ์ƒํ•˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ํ• ๋‹น์„ํ•˜๋˜ ๋ณต์‚ฌ๋ฅผํ•˜๋˜ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

tt1 = (1, 2, 3, 4, 5)
tt2 = tuple(tt1)  # ๐Ÿ‘ˆ ์–•์€ ๋ณต์‚ฌ
tt3 = tt1[:]
print(id(tt1), id(tt2), id(tt3)) # 140317351802896 140317351802896 140317351802896
print(tt1 is tt2) # True
print(tt1 is tt3) # True

โœ”๏ธ ์‹ฌ์ง€์–ด str, tuple ๋“ฑ ๋ถˆ๋ณ€ํ˜•์€ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ๊ฐ™์€ ๊ฐ’์ด๋ฉด ์•„์˜ˆ ๋ณ„๊ฐœ๋กœ ์ƒ์„ฑํ•ด๋„ ๊ฐ™์€ ๊ณณ์„ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

tt4 = (10, 20, 30, 40, 50)
tt5 = (10, 20, 30, 40, 50)
ss1 = "Apple"
ss2 = "Apple"
print(tt4 is tt5, tt4 == tt5) # True True
print(id(tt4), id(tt5)) # 140323132363296 140323132363296
print(ss1 is ss2, ss1 == ss2) # True True
print(id(ss1), id(ss2)) # 140323162495920 140323162495920
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€