1 ๋ถ„ ์†Œ์š”

*, ** ๊ฐ€ ๋ญ˜๊นŒ ์ง„์งœ?

*๋Š” C์—์„œ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ž์ฃผ ๋ดค๋‹ค.

ํŒŒ์ด์ฌ์—์„œ๋„ *์ด ๋ณด์ด๋‹ˆ ์‚ด์ง ๋‹นํ™ฉํ–ˆ์ง€๋งŒ, ์ด๊ฒŒ ํฌ์ธํ„ฐ์ฒ˜๋Ÿผ ์ฃผ์†Œ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์•ˆ์‹ฌํ–ˆ๋‹ค.

๋จผ์ € *๊ฐ€ 1๊ฐœ ์“ฐ์ด๋Š” *args์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

*args?

*args๋Š” *arguments์˜ ์ค„์ž„๋ง์ด๋‹ค.

์ฆ‰, *args๊ฐ€ ์•„๋‹Œ *doc ์ด๋Ÿฐ ์‹์œผ๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„์ด ์™€๋„ ๋œ๋‹ค.

์ด *args๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ž๋ฅผ ํ•จ์ˆ˜๋กœ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค!

์˜ˆ์‹œ

def peoples(*args):
    for name in args:
        print(name)
    print(type(args))
		    

people_names = ["์งฑ๊ตฌ", "๋งน๊ตฌ", "์ฒ ์ˆ˜", "ํ›ˆ์ด", "์œ ๋ฆฌ"]

peoples(people_names)

์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

['์งฑ๊ตฌ', '๋งน๊ตฌ', '์ฒ ์ˆ˜', 'ํ›ˆ์ด', '์œ ๋ฆฌ']
<class 'tuple'>

์ด์ฒ˜๋Ÿผ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ„ ๊ฐ’(args)์€ ํŠœํ”Œ๋กœ ์ธ์‹๋˜์–ด ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค!

์ฆ‰, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ *args ์˜ ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š”๊ฑฐ์˜€๋‹ค.

**kwargs?

kwargs๋Š” keyword argument์˜ ์ค„์ž„๋ง๋กœ, ํ‚ค์›Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์˜ˆ์‹œ

def introduce(**kwargs):
    for key, value in kwargs.items():
        print(f"{key} : {value}")

introduce(name="junho")

์ด๋ ‡๊ฒŒ ์ด๋ฒˆ์—๋Š” ์ธ์ž๋ฅผ **kwargs๋กœ ๋ฐ›์•˜๋‹ค.

์ด๊ฑธ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

name : junho

์ฆ‰, **kwargs๋Š” {keyword : value}์˜ ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์—ญํ• ์ด๊ณ , dictionary ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ „๋‹ฌ๋œ๋‹ค.

*args์™€ **kwargs์—๋„ ์ˆœ์„œ๊ฐ€ ์žˆ๋‹ค!

python ๋‚ด๋ถ€์—์„œ๋Š” *args์™€ **kwargs ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐ์—๋„ ์ˆœ์„œ๊ฐ€ ์žˆ๋‹ค!

val > *args > **kwargs ์ˆœ์„œ๋‹ค! (์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐˆ ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„ ๋Šฆ์Œ)

์˜ˆ์‹œ

def func1(name, *args, **kwargs):
		pass

def func2(**kwargs, *args):
		pass

def func3(*args, name):
		pass
  • func1: OK
  • func2: NO
  • func3: NO

์ฐธ๊ณ  ์ž๋ฃŒ

[๋‚˜๋ฆ„ ์ค‘๊ธ‰ ํŒŒ์ด์ฌ1] *args์™€ **kwargs

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ