[๐Ÿš€ Check Point] 28. Function Parameters

jeong_hyeonยท2022๋…„ 3์›” 2์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
4/22
post-thumbnail

Q1. ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ• ๋•Œ default value parameter๋ฅผ non-default value parameter ์•ž์— ์ •์˜ ํ•˜๋ฉด ์•ˆ ๋˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด๋ณด์‹œ๊ณ  ๋ธ”๋กœ๊น… ํ•ด์ฃผ์„ธ์š”.

  • default value parameter :
    ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋ ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ธ์ž๋ฅผ ๋ฐ›์•„ ๋„˜๊ฒจ์ค€๋‹ค.

  • non-default value parameter :
    ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋ ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ธ์ž๋ฅผ ๋ฐ›์•„ ๋„˜๊ฒจ์ค„ ์ˆ˜ ๋„ ์žˆ์œผ๋ฉฐ,
    ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์•„๋„ keywordname=value ํ˜•์‹์œผ๋กœ ์„ ์–ธ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ง€์ •ํ•œ value ๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์ œ
์•„๋ž˜์˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด number๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€์žˆ๊ณ  my ์™€ your๋ฅผ print ํ•ด์ค€๋‹ค.

์œ„์˜ 5๋ฒˆ์งธ์ค„์„ ๋ณด๋ฉด my = '1234','567'์ด๋ผ๋Š” non-default value parameter๋ฅผ ๋ฐ›๊ฒŒ ๋˜๊ณ  SyntaxError: positional ์ด๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธด๊ฒƒ์„ ๋ณผ์ˆ˜์žˆ๋‹ค.
(๋ฐ”๋กœ ์•„๋ž˜๋Š” ์˜คํƒ€..)
ํ•˜์ง€๋งŒ 9๋ฒˆ์งธ ์ค„์„ ๋ณด๋ฉด number(my = 1234, your = 5678) ์ผ๊ฒฝ์šฐ
non-default value parameter ํ˜•์‹์œผ๋กœ ์ง€์ •์ด ๋˜์–ด์žˆ๋”๋ผ๋„ keywordname = value ํ˜•์‹์œผ๋กœ ์ง€์ •์ด ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰์ด ๋œ๋‹ค.
(number(your = 5678 , my = 1234)๋กœ ์ง€์ •๋˜์–ด๋„ ์‹คํ–‰๋จ)
๋ฌผ๋ก  ๋งˆ์ง€๋ง‰๊ณผ ๊ฐ™์ด default value parameter ๋กœ ์ž‘์„ฑ์„ ํ•ด๋„ ๋ฌธ์ œ๋Š” ์—†๋‹ค.

๊ฒฐ๋ก 
-> ํŒŒ์ด์ฌ์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š” ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

def example(a,b,c=None, r="w",d=[],ae,**ab)
a,b : positional parameter
c=none : optional parameter
r="w" : keyword parameter
d = [] : list parameter
ae : keyword-only
**ab : var-keyword parameter
ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•˜๋Š” ์ˆœ์„œ ์—†์ด ๋ฌด์ž‘์œ„๋กœ ์ƒ์„ฑ ์‹œ ํ•จ์ˆ˜๋Š” ์–ด๋–ค ํŒŒ๋ผ๋ฏธํ„ฐ์— ์–ด๋–ค ๊ฐ’์„ ๋ฐ›์•„์•ผ ๋ ์ง€ ์ •ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ ์ด๋‹ค.

์ด ์ˆœ์„œ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Q2.์œ„์น˜ ์ธ์ˆ˜(positional arguments)์™€ ๊ฐ€๋ณ€ ์ธ์ˆ˜(variable length arguments)์˜ ์œ„์น˜๋ฅผ ์ฐธ๊ณ  ์ž๋ฃŒ๋ฅผ ํ†ตํ•ด ์กฐ์‚ฌํ•ด์„œ ์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์—๋Ÿฌ๋ฅผ ๊ณ ์ณ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ•ด ์ฃผ์„ธ์š”.

์œ„์น˜ ์ธ์ˆ˜๊ฐ€ parameter์— ํ• ๋‹น๋  ๋•Œ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ ์ˆœ์„œ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ์กด์žฌ

์œ„์น˜ ์ธ์ˆ˜๋ž€?
ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ• ๋•Œ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์ด ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ˆœ์„œ๋Œ€๋กœ ์ „๋‹ฌ๋˜๋Š” ์ธ์ˆ˜๋ฅผ ์˜๋ฏธ

๊ฐ€๋ณ€์ธ์ˆ˜๋ž€ ๋ฌด์—‡์ธ๊ฐ€ ?
์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š ์˜๋ฏธ์ด๋ฉฐ, ๊ฐ€๋ณ€์ธ์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜๋ž€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

*arg๋ฅผ ์‚ฌ์šฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ• ๋• ์ˆœ์„œ๋„ ์ค‘์š”ํ•˜๋‹ค.

์šฐ์„ ์ˆœ์œ„
1. ์œ„์น˜ ์ธ์ˆ˜ (default X)
2. ์œ„์น˜ ์ธ์ˆ˜ (default O)
3. / (์œ„์น˜ ์ธ์ˆ˜๋“ค ์–ด๋”˜๊ฐ€์— ์กด์žฌ)
4. ์œ„์น˜ ๊ฐ€๋ณ€ ์ธ์ˆ˜ (*)
5. ํ‚ค์›Œ๋“œ ์ „์šฉ ์ธ์ˆ˜ (๊ธฐ๋ณธ๊ฐ’ ์œ ๋ฌด ์ƒ๊ด€ ์—†์Œ)
6. ํ‚ค์›Œ๋“œ ์ „์šฉ ๊ฐ€๋ณ€ ์ธ์ˆ˜ (**)

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

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

    
func_param_with_var_args("์ •์šฐ์„ฑ", "01012341234", "seoul", 20)
TypeError: func_param_with_var_args() missing 1 required keyword-only argument: 'age'

age๊ฐ’์ด *arg๊ฐ’์ด๋ž‘ ๋ฐ”๋€Œ์–ด์„œ ์ •์ƒ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์˜ error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ดage๊ฐ’๊ณผ *arg ๊ฐ’์„ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ” ํ˜ธ์ถœํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

def func_param_with_var_args(name,age,*args):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)
    
func_param_with_var_args("์ •์šฐ์„ฑ", 20, "01012341234", "seoul")


์œ„์™€ ๊ฐ™์ด ์ •์ƒ ์ž‘๋™ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ• ์ˆ˜์žˆ๋‹ค.

Q3.๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜ (variable length keyword arguments)์˜ ์œ„์น˜๋ฅผ ์ฐธ๊ณ  ์ž๋ฃŒ๋ฅผ ํ†ตํ•ด ์กฐ์‚ฌํ•ด์„œ ์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์—๋Ÿฌ๋ฅผ ๊ณ ์ณ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ•ด ์ฃผ์„ธ์š”.

def func_param_with_kwargs(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)

func_param_with_kwargs("์ •์šฐ์„ฑ", "20", mobile="01012341234", address="seoul")


์œ„์™€ ๊ฐ™์ด ์˜ค๋ฅ˜ ๋ฐœ์ƒ

def func_param_with_kwargs(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)

func_param_with_kwargs("์ •์šฐ์„ฑ", "20", , address="seoul", mobile="01012341234")

์œ„์™€ ๊ฐ™์ด parameter์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พผ ๋’ค argument์˜ ์ˆœ์„œ๋ฅผ ์ง€์ผœ์•ผ ํŒŒ์ด์ฌ์ด ์–ด๋Š ๋ถ€๋ถ„์ด ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜์ธ์ง€ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Q4.์œ„์น˜ ์ธ์ˆ˜์™€ ํ‚ค์›Œ๋“œ ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜ (variable length keyword arguments)์˜ ์œ„์น˜๋ฅผ ์œ„์น˜๋ฅผ ์ฐธ๊ณ  ์ž๋ฃŒ๋ฅผ ํ†ตํ•ด ์กฐ์‚ฌํ•ด์„œ ์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์—๋Ÿฌ๋ฅผ ๊ณ ์ณ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ•ด ์ฃผ์„ธ์š”.

def mixed_params(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)

mixed_params(20, "์ •์šฐ์„ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul")

๋ฐœ์ƒ
๋ฐœ์ƒ ์ด์œ  :
parameter ๊ฐ€
(name="์•„์ด์œ ", *args, age, **kwargs, address)
์ธ๋ฐ
argument๊ฐ€
(20, "์ •์šฐ์„ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul")

ํ•จ์ˆ˜์˜ parameter ์ˆœ์„œ๋ฅผ ์ง€์ผœ parameter ์„ ์–ธ์„ ํ•œ ๋’ค argument ์ „๋‹ฌ ์‹œ์—๋„ ๊ทธ ํ˜•์‹์„ ๋”ฐ๋ผ์•ผ ํ•จ์œผ๋กœ

def mixed_params(age, name="์•„์ด์œ ", *args, 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)


mixed_params(20, "์ •์šฐ์„ฑ", "01012341234", "male", address="seoul", mobile="01012341234")

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค!

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