[Python] Function parameters

byhazyยท2021๋…„ 6์›” 10์ผ
0

Python

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

๐Ÿ“Œ Function parameters

  • ํ•จ์ˆ˜๋Š” ํŒŒ๋ฆฌ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋ฆฌํ„ด๊ฐ’์„ ์•„์›ƒ์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฑด๋„ค์ฃผ์–ด์„œ ํ˜ธ์ถœํ•œ๋‹ค.

- ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํ˜•ํƒœ

  • 1. ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์ด ์ „ํ•ด์งˆ ๋•Œ
    -> position arguments๋ผ๊ณ  ํ•จ
def love(food,friend):
    print(f"{food} and {friend}")

love("ํฌ์ผ€","ํ™๊ธธ๋™")

#๊ฒฐ๊ณผ 
ํฌ์ผ€ and ํ™๊ธธ๋™
  • 2.์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์ด ์ „ํ•ด์ง€์ง€ ์•Š์„ ๋•Œ
    • ์ˆœ์„œ๋Œ€์‹ ์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๋งž์ถ”์–ด ๊ฐ’์„ ์ „๋‹ฌํ•จ.
    • ๊ฐ€๋…์„ฑ์ด ์ข‹๊ณ  ์ˆœ์„œ๋ฅผ ํ‹€๋ ค ๊ฐ’์„ ํ•จ์ˆ˜์— ๋„˜๊ฒจ๋„ ์‹ค์ˆ˜๋กœ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์Œ.
      -> keyword arguments๋ผ๊ณ  ํ•จ
def love(food,friend):
    print(f"{food} and {friend}")

love(friend= "ํ™๊ธธ๋™", food = "ํฌ์ผ€")

#๊ฒฐ๊ณผ 
ํฌ์ผ€ and ํ™๊ธธ๋™
  • 3.position argument ์™€ keyword arguments๊ฐ€ ์„ž์˜€์„ ๋•Œ
    • ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•  ๋•Œ์—๋Š” keyword argument์˜ ์ˆœ์„œ๋Š” ์ƒ๊ด€ ์—†์ง€๋งŒ, position argument์˜ ์ˆœ์„œ๋Š” ๊ผญ ์ง€์ผœ์ค˜์—ฌ ํ•œ๋‹ค. ๋งŒ์•ฝ ์ง€ํ‚ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
def love(food,friend):
    print(f"{food} and {friend}")

love("ํฌ์ผ€", friend="ํ™๊ธธ๋™")

#๊ฒฐ๊ณผ 
ํฌ์ผ€ and ํ™๊ธธ๋™
  • 4.ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋””ํดํŠธ ๊ฐ’์„ ์ •์˜ํ•  ๋•Œ
    • ๋””ํดํŠธ ๊ฐ’์ด ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœํ•  ๋•Œ ๊ฐ’์„ ๋„˜๊ธฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋‹ค๋งŒ, ๋””ํดํŠธ ๊ฐ’์ด ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ณด๋‹ค ์•ž์— ์œ„์น˜ํ•ด ์žˆ์œผ๋ฉด ์•ˆ ๋œ๋‹ค!
def love(food,friend="ํ™๊ธธ๋™"):
    print(f"{food} and {friend}")

love("ํฌ์ผ€")

#๊ฒฐ๊ณผ
ํฌ์ผ€ and ํ™๊ธธ๋™

โžก๏ธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ• ๋•Œ default value parameter๋ฅผ non-default value parameter ์•ž์— ์ •์˜ ํ•˜๋ฉด ์•ˆ ๋˜๋Š” ์ด์œ ?

  • ๋งŒ์•ฝ position arguments๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ• ๋‹น๋  ๋•Œ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ• ๋‹น๋œ๋‹ค. ์ฆ‰ ์‹œ์ž‘์ ์ด ์กด์žฌํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋””ํดํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์•ž์— ์ •์˜ํ•˜๋ฉด ํ•จ์ˆ˜๋Š” ๊ฐ’์„ ์–ด๋””์— ์ •์˜ํ•ด์•ผํ•˜๋Š”์ง€ ์•Œ์ˆ˜๊ฐ€ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋””ํดํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์€ ํŒŒ๋ผ๋ฏธํ„ฐ ์•ž์— ์œ„์น˜ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

- ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์šฐ์„ ์ˆœ์œ„

  1. Regular Positional Arguments (์ˆœ์„œ)
  2. Default Arguments (ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋””ํดํŠธ)
  3. Variable Length Positional Arguments (*args)
  4. Non-Default Keyword-Only Arguments (ํ•จ์ˆ˜์˜ ์„ ์–ธ์—์„œ ๋””ํดํŠธ ๊ฐ’์ด ์—†์Œ, ํ‚ค์›Œ๋“œ ์กด์žฌ)
  5. Keyword-Only Arguments With Defaults (ํ•จ์ˆ˜ ์„ ์–ธ์—์„œ ๋””ํดํŠธ ๊ฐ’์ด ์กด์žฌ)
  6. Variable Length Keyword Arguments (kwargs)

(ํ‚ค์›Œ๋“œ์™€ ๋””ํดํŠธ ์ž˜ ํ™•์ธํ•˜๊ธฐ. ํ•จ์ˆ˜์˜ ์„ ์–ธ์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋„ฃ๋Š”๊ฒŒ ๋””ํดํŠธ ๊ฐ’!!!)


๐Ÿ“Œ ๋ฌธ์ œ

- positinoal arguments & variable length arguments

  • positinoal arguments(์œ„์น˜ ์ธ์ˆ˜) vs variable length arguments(๊ฐ€๋ณ€ ์ธ์ˆ˜ = *args)
  • *args์ฐธ๊ณ 
  • *args๋Š” ์ž…๋ ฅ๊ฐ’์ด ๋ช‡๊ฐœ์ธ์ง€ ๋ชจ๋ฅผ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ ์ž…๋ ฅ๊ฐ’์„ ์ „๋ฌธ ๋ชจ์•„ ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
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)

#์—๋Ÿฌ ๋ฐœ์ƒ

โžก๏ธ ์œ„์˜ ํ•จ์ˆ˜๋ฅผ ๋ณด๋ฉด name์—๋Š” ์ •์šฐ์„ฑ, args์—๋Š” ๋ฒˆํ˜ธ์™€ ์ง€์—ญ, age์—๋Š” ๋‚˜์ด๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค. ์ด๋ฅผ ์œ„์˜ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์šฐ์„ ์ˆœ์œ„๋กœ ๋ณด๋ฉด,
name์€ Regular Positional Arguments์ด๊ณ ,
args๋Š” Variable Length Positional Arguments,
age๋Š” Regular Positional Arguments๋กœ ์ •์˜๋ฅผ ํ•˜๋Š” ์ ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ,

ํ˜ธ์ถœ์„ ํ• ๋•Œ ์ž…๋ ฅ๊ฐ’์ด ๋ช‡๊ฐœ์ธ์ง€ ๋ชจ๋ฅผ๋•Œ ์‚ฌ์šฉํ•˜๋Š” *args๊ฐ€ ์ž…๋ ฅ๊ฐ’์„ ๋ฒˆํ˜ธ, ์„œ์šธ ๊ทธ๋ฆฌ๊ณ  20๊นŒ์ง€ ๋ฐ›์•„๋“ค์—ฌ ๊ฐ€๋ณ€์˜ ๊ฐ’์œผ๋กœ ๋ฐ›์•„๋“ค์—ฌ, ์ •์ž‘ age์—๊ฒŒ ๋“ค์–ด๊ฐˆ ๊ฐ’์ด ์—†์–ด์„œ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธด๋‹ค.

  • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•
# 1. age= 20 ํ‚ค์›Œ๋“œ๋ฅผ ๋งŒ๋“ฌ.
# Regular Positional Arguments ๋‹ค์Œ Variable Length Positional Arguments ๋‹ค์Œ Non-Default Keyword-Only Arguments 
func_param_with_var_args("์ •์šฐ์„ฑ", "01012341234", "seoul", age = 20)


# 2.age์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พผ๋‹ค. 
# -> Regular Positional Arguments ๋จผ์ € ์ถœ๋ ฅํ•˜๊ณ , Variable Length Positional Arguments 
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")

- variable length keyword arguments

  • variable length keyword arguments(๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜ = **kwargs)
  • **kwargs ์ฐธ๊ณ 
  • **kwargs๋Š” ํ‚ค์›Œ๋“œ = ๊ฐ’ ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๋”•ํŠธ ํ˜•ํƒœ 'key' : 'value'๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ „๋‹ฌํ•œ๋‹ค.
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")

#์˜ค๋ฅ˜ ๋ฐœ์ƒ 
# ์ฐธ๊ณ ๋กœ ๋””ํดํŠธ๊ฐ€ ์žˆ์–ด๋„ ํ‚ค์›Œ๋“œ๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์Œ.

โžก๏ธ name์€ Regulal Positional Arguments,
age๋„ Regular Positional Arguments,
**kwargs๋Š” Variable Length Keyword Arguments,
address๋Š” Keyword-Only Arguments With Defauls.

๋”ฐ๋ผ์„œ variable length keyword arguments๊ฐ€ Keyword-Only Arguments With Defauls๋ณด๋‹ค ์•ž์— ์žˆ์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • ํ•ด๊ฒฐ๋ฐฉ๋ฒ•
# 1. **kwargs๋ฅผ ๋งจ ๋’ค์— ๋†“๋Š”๋‹ค. 
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", mobile="01012341234", address="seoul")

- positional arguments & 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")

#์˜ค๋ฅ˜ ๋ฐœ์ƒ

โžก๏ธ ์ˆœ์„œ๋ฅผ ๋ณด๋ฉด

name="์•„์ด์œ " ๋Š” Default Arguments,
*args๋Š” Variable Length Positional Arguments,
age๋Š” Regular Positional Arguments,
**kwargs๋Š” Variable Length Keyword Arguments,
address๋Š” Non-Default Keyword-Only Arguments

  • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์œ„๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์šฐ์„ ์ˆœ์œ„ ์ˆœ์œผ๋กœ ๋ฐ”๊พผ๋‹ค.
์ฆ‰ age, name="์•„์ด์œ ", *args, address, **kwargs ์ˆœ์„œ

# ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์šฐ์„ ์ˆœ์œ„๋Œ€๋กœ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟˆ
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" ,mobile="01012341234", address="seoul")

# ์ •์ƒ ์ถœ๋ ฅ!!!!!

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