ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/Python

ํŒŒ์ด์ฌ ํŒจํ‚น(packing)๊ณผ ์–ธํŒจํ‚น(unpacking)์˜ ๋ชจ๋“  ๊ฒƒ

์„œ์•„๋ž‘๐Ÿ˜ 2023. 12. 17. 22:48

 

 

์˜ค๋Š˜ ์†Œ๊ฐœํ•˜๋Š” ํŒจํ‚น๊ณผ ์–ธํŒจํ‚น์€ ๋ฐ์ดํ„ฐ๋ณด๋‹ค๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ์ธ์ž(Arguments) ์ค‘์‹ฌ์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

 

 

ํŒจํ‚น(Packing)

printํ•จ์ˆ˜๋Š” ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ์ธ์ž๊ฐ€ ๋ช‡ ๊ฐœ๋“  ๊ฐ„์— ์ƒ๊ด€ํ•˜์ง€ ์•Š๊ณ  ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.

print("Hello")
print("He", "llo")
print("He", "ll", "o")

# Hello
# Hello
# Hello

 

 

ํ•จ์ˆ˜๊ฐ€ ๋ฐ›์„ ์ธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•ด๋†“์ง€ ์•Š์•˜์„ ๋•Œ(๊ฐ€๋ณ€ ์ธ์ž), ์ž…๋ ฅ๋ฐ›์€ ์ธ์ž๋“ค์„ ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•ด์•ผ ํ• ๊นŒ์š”? ๋ฐ”๋กœ packing์ด๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ ๊ด€๋ฆฌํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํŒจํ‚น์€ ์ธ์ž๋กœ ๋ฐ›์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ํ•ฉ์ณ์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€๋ณ€์ธ์ž ํŒจํ‚น์€ *ํ•œ ๊ฐœ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜ ์•ž์— ๋ถ™์ž„์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

def func(*args):
      print(args)
      print(type(args))

 

๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„ ์•ž์— *์„ ๋ถ™์—ฌ์ค€๋‹ค๋ฉด, ๊ฐ€๋ณ€์ธ์ž๋กœ ๋ณด๋‚ธ ๋ชจ๋“  ๊ฐ์ฒด๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ๊ด€๋ฆฌํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€๋ณ€์ธ์ž๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์€ ๋‹จ์ˆœ ์ธ์ž๋“ค์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” 1, 2, 3, 4, 5, 6, 'a', 'b'๊ฐ€ ๋ชจ๋‘ ๊ฐ€๋ณ€์ธ์ž์ž…๋‹ˆ๋‹ค. ์ถ”ํ›„์— ๋‚˜์˜ฌ ํ‚ค์›Œ๋“œ์ธ์ž์— ๋น„ํ•ด ๋‹จ์ˆœํ•˜๊ฒŒ ์ž…๋ ฅ๋ฐ›์€ ์ธ์ž๋“ค์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ณ€์ธ์ž๋“ค์€ ๋ชจ๋‘ ํŠœํ”Œ๋กœ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

func(1, 2, 3, 4, 5, 6, 'a', 'b')


# (1, 2, 3, 4, 5, 6, 'a', 'b')
# <class 'tuple'>

 

์ด๋Ÿฌํ•œ packing์„ ํ†ตํ•ด ๋ฐ›์€ ๋ชจ๋“  ์ˆซ์ž๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช‡ ๊ฐœ๋“  ์ƒ๊ด€์—†์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

def sum_all(*numbers):
      result = 0
for number in numbers:
           result += number
return result

print(sum_all(5, 6, 7, 8)) # 26
print(sum_all(10, 11, 12, 13, 14, 15)) # 75

 

packing์„ ์ด์šฉํ•ด์„œ ๋ฐ˜๋“œ์‹œ ๋ฐ›์•„์•ผ ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

def printCityName(capital, sec_capital, *cities):
	print("์ˆ˜๋„ :", capital)
    print("์ œ2 ์ˆ˜๋„ :", sec_capital)
	if cities:
    	print("๋‹ค๋ฅธ ์ง€์—ญ๋“ค:")
		for city in cities:
        	print(city)

printCityName('์„œ์šธ', '๋ถ€์‚ฐ', '๋Œ€์ „', '์ธ์ฒœ', '๋Œ€๊ตฌ', '์šธ์‚ฐ', '๊ด‘์ฃผ')

# ์ˆ˜๋„ : ์„œ์šธ
# ์ œ2 ์ˆ˜๋„ : ๋ถ€์‚ฐ
# ๋‹ค๋ฅธ ์ง€์—ญ๋“ค:
# ๋Œ€์ „
# ์ธ์ฒœ
# ๋Œ€๊ตฌ
# ์šธ์‚ฐ
# ๊ด‘์ฃผ

๊ฐ€๋ณ€์ธ์ž๊ฐ€ ํŒจํ‚นํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋‚˜๋ฉด ๊ทธ ์ดํ›„์— ์ธ์ž๊ฐ€ ๋ช‡ ๊ฐœ์ด๋˜์ง€, tuple๋กœ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๊ฐ€ ๋˜์–ด์„œ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

 

๋™์ผํ•˜๊ฒŒ ํ‚ค์›Œ๋“œ ์ธ์ž์— ํŒจํ‚น์€ **์„ ํ†ตํ•ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

def kwpacking(**kwargs):
     print(kwargs)
     print(type(kwargs)

kwpacking(a=1, b=2, c=3)


# {'a': 1, 'b': 2, 'c': 3}
# <class 'dict'>

 

ํ‚ค์›Œ๋“œ ์ธ์ž๋Š” ํŒจํ‚นํ•œ ์ธ์ž๋“ค์„ ํ‚ค์›Œ๋“œ์™€ ์ธ์ž ์Œ์œผ๋กœ ์ด๋ค„์ง„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

printCityNameํ•จ์ˆ˜๋ฅผ ํ‚ค์›Œ๋“œ ์ธ์ž๋กœ ์ถœ๋ ฅํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

def printCityName(capital, sec_capital, **cities):
    print("์ˆ˜๋„ :", capital)
    print("์ œ2 ์ˆ˜๋„ :", sec_capital)
    if cities:
        print("๋‹ค๋ฅธ ์ง€์—ญ๋“ค:")
        for title, city in cities.items():
            print(f"{title} - {city}")

printCityName('์„œ์šธ', '๋ถ€์‚ฐ', ๊น€ํƒœ๊ท ์˜๊ณ ํ–ฅ='๋Œ€์ „', ๋ฅ˜ํ˜„์ง„์˜๊ณ ํ–ฅ='์ธ์ฒœ', ๋ฐ•์„๋ฏผ์˜๊ณ ํ–ฅ='๋Œ€๊ตฌ', ๊ตฌ์ฐฝ๋ชจ์˜๊ณ ํ–ฅ='์šธ์‚ฐ', ๊น€๋ณ‘ํ˜„์˜๊ณ ํ–ฅ='๊ด‘์ฃผ')

# ์ˆ˜๋„ : ์„œ์šธ
# ์ œ2 ์ˆ˜๋„ : ๋ถ€์‚ฐ
# ๋‹ค๋ฅธ ์ง€์—ญ๋“ค:
# ๊น€ํƒœ๊ท ์˜๊ณ ํ–ฅ - ๋Œ€์ „
# ๋ฅ˜ํ˜„์ง„์˜๊ณ ํ–ฅ - ์ธ์ฒœ
# ๋ฐ•์„ ๋ฏผ์˜๊ณ ํ–ฅ - ๋Œ€๊ตฌ
# ๊ตฌ์ฐฝ๋ชจ์˜๊ณ ํ–ฅ - ์šธ์‚ฐ
# ๊น€๋ณ‘ํ˜„์˜๊ณ ํ–ฅ - ๊ด‘์ฃผ

 

 

 

 

unpacking

packing์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ํ•˜๋‚˜์˜ ๊ฐ์ฑ„๋กœ ๋ฌถ์–ด์ฃผ๋Š” ๊ฐœ๋…์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๋ฐ˜๋Œ€๋˜๋Š” ๊ฐœ๋…์ธ unpacking์€ ๋ฌถ์—ฌ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์œผ๋กœ ํ’€์–ด์ฃผ๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

ํ•จ์ˆ˜์—์„œ unpacking์„ ํ•  ๋•Œ๋Š”, ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ *์„ ๋ถ™์ด๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์ธ์ž ์•ž์— *์„ ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

๋™์ผํ•˜๊ฒŒ ๊ฐ€๋ณ€์ธ์ž๋ฅผ unpacking ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” *๋ฅผ, ํ‚ค์›Œ๋“œ์ธ์ž๋ฅผ unpackingํ•˜๋Š” ๊ฒฝ์šฐ **๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

def sum(a, b, c):
	return a + b + c

numbers = [1, 2, 3]
sum(numbers) # error

print(sum(*numbers)) # ์ถœ๋ ฅ : 6

[1, 2, 3]์„ ์ธ์ž๋กœ ๋ณด๋‚ผ ๋•Œ, *์„ ๋ถ™์ด๋ฉด unpacking์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

unpacking์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

1. sum(*numbers)
2. sum(*[1, 2, 3])
3. sum(1, 2, 3)

 

unpacking์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ธ์ž๋ฅผ ํ•ด์ฒดํ•˜๋Š” ๊ฐœ๋…์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด์ฒด๋œ ๊ฒฐ๊ณผ๊ฐ€ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ๋‹ค๋ฅด๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

sum(*[1, 2, 3, 4])


TypeError:sum() takes 3 positional arguments but 4 were given

 

 

๊ฐ€๋ณ€์ธ์ž๋ฅผ unpackingํ•  ๋•Œ๋Š” listํƒ€์ž… ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, Container๊ฐ์ฒด๋ผ๋ฉด ๋‹ค ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

sum(*'abc') # 'abc'
sum(*(4, 5, 6)) # 15
sum(*{'๊ฐ€', '๋‚˜', '๋‹ค'}) # '๊ฐ€๋‚˜๋‹ค'
sum(*{'์น˜ํ‚จ': 3, 'ํ”ผ์ž': 12, '์Œ๋ฃŒ์ˆ˜': 10}) # '์น˜ํ‚จํ”ผ์ž์Œ๋ฃŒ์ˆ˜'

setํƒ€์ž…๊ณผ dictํƒ€์ž…์€ ์ˆœ์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ํ‚ค์›Œ๋“œ์ธ์ž๋กœ unpackingํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. unpackingํ•˜๊ธฐ ์œ„ํ•ด์„  ์ธ์ž๊ฐ€ key์™€ ์ธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” mappingํƒ€์ž…, ์ฆ‰ dict๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

def cal(first, op, second):
	if op == '+':
		return first + second
	if op == '/':
		return first / second
	if op == '-':
		return first - second
	if op == '*':
		return first * second

obj = {
  'first': 11,
  'second': 33,
  'op': '*'
}

cal(**obj) 

# ๊ฒฐ๊ณผ : 363

์œ„ ์˜ˆ์ œ์— ํ‚ค์›Œ๋“œ ์ธ์ž์˜ unpacking์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

1. cal(**obj)
2. cal(obj = {
  'first': 11,
  'second': 32,
  'op': '*'
})
3. cal(first=11, second=33, op='*')

๊ฐ€๋ณ€์ธ์ž์˜ unpacking์ฒ˜๋Ÿผ unpacking๋˜๋Š” ์ธ์ž๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํ‚ค์›Œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ๋น„์–ด์žˆ๋Š” ์ธ์ž๋ฅผ unpacking๋ฅผ ํ•˜๋ฉด ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜์˜ packing๊ณผ unpacking์„ ์ด์šฉํ•˜์—ฌ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์–ด๋– ํ•œ ํ•จ์ˆ˜์—๋„ ๋ฐ˜์‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•จ์ˆ˜์™€ ํ•จ์ˆ˜์— ์ธ์ž๋“ค์„ ๋ฐ›์•„์„œ ์‹œ์ž‘ ์ „ ์•Œ๋ฆผ๊ณผ ํ•จ๊ป˜ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ฃผ๋Š” ๋ž˜ํ•‘(wrapping) ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

def start(func, *args, **kwargs):  # packing ์‚ฌ์šฉ
    print("ํ•จ์ˆ˜๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.")
    return func(*args, **kwargs)  # unpacking ์‚ฌ์šฉ
    

def printAny(*args, **kwargs):	# ๋ž˜ํ•‘์— ์‚ฌ์šฉํ•  ํ•จ์ˆ˜
    if args:
        for value in args:
            print(value)
            
    if kwargs:
        for title, value in kwargs.items():
            print(f"{title} - {value}")
            
start(printAny, 1, 5, ์ด๋ฆ„1='์„œ์•„', ์ด๋ฆ„2='๋ž‘') # ํ˜ธ์ถœ


# ํ•จ์ˆ˜๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
# 1
# 5
# ์ด๋ฆ„1 - ์„œ์•„
# ์ด๋ฆ„2 - ๋ž‘

 

 

packing๊ณผ unpacking์€ ํŒŒ์ด์ฌ์˜ ๊ฐ„๊ฒฐํ•˜๊ณ  ์œ ์—ฐํ•œ ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.