# ๐ Variable Length Arguments

Min-Ho Leeยท2020๋ 6์ 25์ผ
0

๋ชฉ๋ก ๋ณด๊ธฐ
13/22

Thursday_June_25
.
.
.

# ๐ Variable Length Arguments (๊ฐ๋ณ ์ธ์)

Positional Arguments ํน์! Keyword Arguments๋ฐฉ์์ผ๋ก
Parameter์๊ฒ ๋๊ฒจ์ค ๊ฐ์ด ๋๋ฌด ๋ง๊ฑฐ๋, ๊ฐ์๊ฐ ์ ํด์ง์ง ์์์ ๋
์ฌ์ฉ์ ํ๋ค.

๊ทธ๋์ ๊ฐ๋ณ์ธ์์ ์ข๋ฅ๋ ์ ํํ 2๊ฐ๋ก ๋๋๋ค.

๋ค์ ๋ ๊ฟ๋ง๊ฐ์ ์ค๋ช์ ์ํด ์ฌ์ง์ ๋์ฐ๊ฒ ๋ค.

## 1๏ธโฃ Variable Length Positional Arguments

์์์ ๊ฐ์์ Positional Arguments ๋ฐฉ์์ผ๋ก, Tuple ํํ์ ๋ณ์๋ก ์ ์ฅํ๋ค.

EX)

def test(a, b, c, *args):
print(a)
print(b)
print(c)
print(args)

test(1, 2, 3, 4, 5, 6)

ํจ์๋ฅผ ์ ์ํ๊ณ  ๋ค์๊ณผ ๊ฐ์ด ํธ์ถ์ ํ๋ฉด
Positional Arguments ๋ฐฉ์์ผ๋ก ๊ฐ์ด ์ ๋ฌ์ด ๋๋ฉด์
๋น์ฐํ a์ b์ c์๋ ๊ฐ๊ฐ 1๊ณผ 2์ 3์ด ์ ๋ฌ์ด ๋  ๊ฒ์ด๊ณ ,
๊ฐ๋ณ์ ๊ธธ์ด์ ๊ฐ์ ๋ฐ๋ args๋ผ๋ Parameter๋ 4์ 5์ 6์
Tuple์ ํํ๋ก ๋ฐ๊ฒ ๋๋ค.
(ํจ์๋ฅผ ์ ์ ํ  ๋๋ง *๋ฅผ ๋ถ์ฌ์ค๋ค.)

์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.

1
2
3
(4, 5, 6)

## 2๏ธโฃ Variable Length Keyword Arguments

์์์ ๊ฐ์์ Keyword Arguments ๋ฐฉ์์ผ๋ก, Dictionary ํํ์ ๋ณ์๋ก ์ ์ฅํ๋ค.

EX)

def test(a, b, **kwargs):
print(a)
print(b)
print(kwargs)
test(1, 2, name = "Lee", age = 23, company = "Wecode")

๋จผ์  ํธ์ถ ๋ถ๋ถ์ 1๊ณผ 2๋ ๋น์ฐํ Positional Argument ๋ฐฉ์์ผ๋ก a์ b์๊ฒ ๊ฐ์ ๋๊ฒจ์คฌ๋ค.

์ฌ๊ธฐ์๋ 1๊ณผ 2๊ฐ a์ b์๊ฒ Positional Argument ๋ฐฉ์์ผ๋ก ๊ฐ์ ์ ๋ฌํด ์ฃผ์๊ณ , ket="value" ์ ํํ๋ก kwargs๋ผ๋ Parameter์๋
name = "Lee", age = 23, company = "Wecode" ๋ผ๋ ๊ฐ์ด Dictionary ํํ๋ก ๋ค์ด๊ฐ๊ฒ ๋๋ค.

(์๊น์ ๊ฐ์ด ์ด ๋ฐฉ์๋ ์ ์ ํ  ๋๋ง ๋ฅผ ๋ถ์ฌ์ค๋ค.)**

์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.

1
2
{'name': 'Lee', 'age': 23, 'company': 'Wecode'}

# ๐ ์ดํดํ๊ธฐ

๋ฌธ์ ๋ค์ ํ๋ฉด์ ๐ Positional Argument, Keyword Argument, Variable Length Positional Argument, Variable Length Keyword Argument์ ๋ํด ์ข ๋ ์ตํ๋ณด๊ฒ ๋ค.

## 1๏ธโฃ ๋ฌธ์ 

def exam1(name, *args, age):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)

exam1("์ ์ฐ์ฑ", "01012341234", "seoul", 20)

### โ ์๋ฌ๊ฐ ๋๋ ์ด์

์ด ๋ฌธ์ ์์๋ Positional Argument ๋ฐฉ์์ผ๋ก
name์๋ ์ ์ฐ์ฑ, args์๋ ๋ฒํธ์ ์ฃผ์๋ฅผ, age์๋ 20์ ์ฃผ๋ ค๊ณ  ํ ๊ฒ ๊ฐ์๋ฐ, ์ผ๋จ ํจ์๋ฅผ ์ ์ํ๋ ๋ถ๋ถ์์๋ ์๋ฌ๊ฐ ์์ ๊ฒ์ด๋ค.

์  ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด
name์ non-default positional parameter์ด๊ณ ,
*args๋ Variable Length Positional Parameter์ด๊ณ ,
age๋ non-default keyword parameter ์ฌ์
์ ์๋ฅผ ํ๋ ๋ฐ์๋ ์๋ฌ๊ฐ ์๋ค.

ํ์ง๋ง ํธ์ถ ๋ถ๋ถ์์ ์๋ชป ๋์๋ค.

args๋ ๊ฐ๋ณ์ ๊ธธ์ด๋ฅผ ์๋ ฅ๋ฐ๋ ๋์ธ๋ฐ,
๋ฒํธ์ ์์ธ, ๊ทธ๋ฆฌ๊ณ  20๊น์ง๋ฅผ ๊ฐ๋ณ์ ๊ฐ์ผ๋ก ์ธ์์ ํด์ 3๊ฐ์ ๊ฐ ๋ชจ๋ ๋ค์ด๊ฒ๊ฐ ๋  ๊ฒ์ธ๋ฐ, ๊ทธ๋ฌ๋ฉด age๋ผ๋ parameter์๊ฒ๋ ๊ฐ์ด ์๋ค.

๊ทธ๋ฆฌ๊ณ  ๋งํ๋ฏ์ด age๋ Keyword๋ก ๊ฐ์ ์ค์ผํ๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋๋ค.

### ๐คฉ ํด๊ฒฐ ๋ฐฉ๋ฒ

1. ํธ์ถ ๋ถ๋ถ์์ 20์ age=20 ์ผ๋ก ๋ฐ๊ฟ์ค๋ค.

def exam1(name, *args, age):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)

exam1("์ ์ฐ์ฑ", "01012341234", "seoul", age = 20)

์ถ๋ ฅ ๊ฒฐ๊ณผ

name=์ ์ฐ์ฑ
args=('01012341234', 'seoul')
age=20

2. age์ 20์ ์๋ฆฌ๋ฅผ ๋ฐ๊ฟ์ค๋ค.

def exam1(name, age, *args):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)

exam1("์ ์ฐ์ฑ", 20, "01012341234", "seoul")

์ถ๋ ฅ ๊ฒฐ๊ณผ

name=์ ์ฐ์ฑ
args=('01012341234', 'seoul')
age=20

Argument์์ ์์๋ฅผ ๋ค๋ฅด๊ฒ ๋๊ฒจ์ฃผ์ด๋ ํจ์์์์ print๋ฌธ ๋ก์ง์ ๋์ผํ๊ธฐ ๋๋ฌธ์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๊ฐ๋ค.

## 2๏ธโฃ ๋ฌธ์ 

def exam2(name, age, **kwargs, address=0):
print("name=",end=""), print(name)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)

exam2("์ ์ฐ์ฑ", "20", mobile="01012341234", address="seoul")

### โ ์๋ฌ๊ฐ ๋๋ ์ด์

์ผ๋จ ์ด ๊ตฌ์กฐ๋ parameter ์์๋๋ก

non-dafault positional
non-dafault positional
variable Length Keyword
default keyword
์ธ๋ฐ,

์ด๊ฑด ํจ์ ์ ์ ์กฐ์ฐจ ์ ๋  ๊ฒ์ด๋ค.

parameter ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด ์๋๋ ์ด์ ๋ฅผ ์ ์ ์๋ค.

### ๐คฉ ํด๊ฒฐ ๋ฐฉ๋ฒ

1 - 1. address parameter๋ฅผ kwargs parameter ์์ผ๋ก ์ฎ๊ฒจ์ค๋ค.

def exam2(name, age, address=0, **kwargs):
print("name=",end=""), print(name)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)

exam2("์ ์ฐ์ฑ", "20", mobile="01012341234", address="seoul")

ํจ์ ์ ์ ๋ถ๋ถ์์๋ ๋ฌธ๋ฒ์ ์ผ๋ก ํ๋ฆฐ ๋ถ๋ถ์ด ์์ผ๋ ํจ์ค.

ํธ์ถ ๋ถ๋ถ์์ argument ๋ถ๋ถ์ ๋ณด๋ฉด,
์ ์ฐ์ฑ๊ณผ 20์ positional ๋ฐฉ๋ฒ์ผ๋ก, mobile๊ณผ address๋ keyword๋ก ์ฃผ์๋ค.
keyword ๋ฐฉ์์ ์์๊ฐ ์๊ด ์๊ธฐ๋๋ฌธ์, ์ ์์ ์ผ๋ก ์๋์ด ๋๋ค.

์ถ๋ ฅ ๊ฒฐ๊ณผ

name=์ ์ฐ์ฑ
age=20
kwargs={'mobile': '01012341234'}
address=seoul

1 - 2.
1๋ฒ๊ณผ ๊ฐ์ ์ ์๋ ํจ์์์, ์ถ๋ ฅ ๋ถ๋ถ๋ง ๋ฐ๊ฟ์ ๋ค๋ฅด๊ฒ ์๋ํ  ์๋ ์๋ค.

parameter๋ address ๊น์ง positional ๋ถ๋ถ ์ด์ด์,
๋ค์๊ณผ ๊ฐ์ด ํธ์ถํ  ์๋ ์๋ค.

def exam2(name, age, address=0, **kwargs):
print("name=",end=""), print(name)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)

exam2("์ ์ฐ์ฑ", "20", "seoul", mobile="01012341234")

์ด๋ ๊ฒ "seoul" argument๋ฅผ positional argument ๋ฐฉ์์ผ๋ก ์ค๋ ๊ฐ๋ฅํ๋ค.

๋ฌผ๋ก  ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๊ฐ๋ค.

์ถ๋ ฅ ๊ฒฐ๊ณผ

name=์ ์ฐ์ฑ
age=20
kwargs={'mobile': '01012341234'}
address=seoul

## 3๏ธโฃ ๋ฌธ์ 

def exam3(name="์์ด์ ", *args, age, **kwargs, address):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)

exam3(20, "์ ์ฐ์ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul")

### โ ์๋ฌ๊ฐ ๋๋ ์ด์

ํจ์๋ฅผ ์ ์ํ  ๋ ๋ถํฐ ์๋ฌ๊ฐ ๋๋ค.
parameter ์์๋๋ก ๋ณด๋ฉด

default positional
variable length positional
non-dafault keyword
variable length keyword
non-dafault keyword
์ธ๋ฐ,

๋ง์ง๋ง parameter์ธ address๋ง age์ kwargs ์ฌ์ด์ ๋ฃ์ด์ฃผ๋ฉด
์ ์ ๋ถ๋ถ์์๋ ์ค๋ฅ๊ฐ ์๋๋ค.

์ถ๋ ฅ ๋ถ๋ถ๋ง positional์ธ์ง keyword ์ธ์ง ์ ์๊ฐํด์ ์ ๋ฌํด์ฃผ๋ฉด ๋๋ค.

### ๐คฉ ํด๊ฒฐ ๋ฐฉ๋ฒ

def exam3(name="์์ด์ ", *args, age, address, **kwargs):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)

exam3(20, "์ ์ฐ์ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul")

์ผ๋จ ์ด๋ ๊ฒ ์ ์ ๋ถ๋ถ์์์ ์๋ฌ๋ง ์์ ํ๊ณ , ์ถ๋ ฅ ๋ถ๋ถ์ผ๋ก ํด๊ฒฐ์ ๋ณด๊ฒ ๋ค.

1. ๐ฉ
๊ณ ์น ๊ฒ ๋๋ฌด ๋ง๋ค.
์ผ๋จ ์์ ์ฒ๋ผ ์๋ชป๋ ์ถ๋ ฅ์ ํ๋ค๊ณ  ๊ฐ์ ์ ํ๋ฉด,
20์ name์ผ๋ก, ์ ์ฐ์ฑ์ ๊ฐ๋ณ parameter์ธ args์ ์ฌ๊ธฐ์ ์ปดํจํฐ๋
๋์ ์ง๊ฐ ์จ๋ค. ๋ฒํธ๋ args์ธ์ง ์ด๋ค argument ๊น์ง args์ ๋ฃ์์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.

exam3("์ ์ฐ์ฑ", "male", "01012341234", age=20, address="seoul", mobile="01012341234")

๋ต์ด ์ด๊ฑฐ ๋ง๊ณ ๋ ์๋ค. ์ ์ผํ ๋ต์ด๋ค. (๋ฌผ๋ก  ๋ด ๋จธ๋ฆฌ์ ๊ธฐ์ค์ด๋ค)

๋ฌผ๋ก  ์ฌ๊ธฐ์ age, address, kwargs ์ด 3๊ฐ์ parameter ๋ผ๋ฆฌ๋
keyword argument ๋ฐฉ์์ผ๋ก ๋๊ฒจ์ฃผ๊ธฐ ๋๋ฌธ์ ์์ด๋ ์๊ด์๋ค.

์ถ๋ ฅ ๊ฒฐ๊ณผ

name=์ ์ฐ์ฑ
args=('male', '01012341234')
age=20
kwargs={'mobile': '01012341234'}
address=seoul
๐ Rabbit can take a rest, but ๐ข turtle can't