๐Ÿ ํŒŒ์ด์ฌ Function Parameters

ํŒŒ์ด์ฌ Function Parameters์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

๐Ÿ‘ default value parameter& non-default value parameter

  • default value parameter : ์ธ์ž๊ฐ’์„ ๋ฏธ๋ฆฌ ์ง€์ •ํ•œ ๊ฒƒ
    ex. def member(member="๊ฐ•์Šฌ๊ธฐ")

  • non-default value parameter: ์ธ์ž๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์€ ๊ฒƒ
    ex. def member(age)

default value parameter๋Š” non-default value parameter ์•ž์— ์œ„์น˜ํ•˜๋ฉด ์•ˆ๋œ๋‹ค!!!

def member(member="๊ฐ•์Šฌ๊ธฐ",age, group)
    return f"{member}์˜ ๋‚˜์ด๋Š” {age}์ž…๋‹ˆ๋‹ค."

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธด๋‹ค.

non-default value parameter๊ฐ€ default argument์•ž์— ์™€์•ผํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

์™œ ์•ˆ๋˜๋Š”๋ฐ?

์ผ๋‹จ ํŒŒ์ด์ฌ์—์„œ๋Š” default value parameter๊ฐ€ non-default value parameter ์•ž์— ์žˆ๋Š” ์ƒํƒœ๋กœ ํ•จ์ˆ˜๊ฐ€ ์ •์˜ ๋˜์ง€ ์•Š๋Š”๋‹ค!
์ฆ‰ ํŒŒ์ด์ฌ์—์„œ ์•„์˜ˆ ํ‹€๋ฆฐ ๋ฌธ๋ฒ•์œผ๋กœ ๊ทœ์ •ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

๋งŒ์•ฝ ํŒŒ์ด์ฌ์—์„œ ์ด๋ ‡๊ฒŒ ์ •์˜ํ•˜๋Š”๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž

def member(member="๊ฐ•์Šฌ๊ธฐ",age, gropup)
    return f"{member}์˜ ๋‚˜์ด๋Š” {age}์ž…๋‹ˆ๋‹ค."

member(28, "๋ ˆ๋“œ๋ฒจ๋ฒณ")

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š”๊ฒŒ member์ธ์ž๋ฅผ ์ง€์ •์„ ํ–ˆ๋Š”์ง€ ์•ˆํ–ˆ๋Š”์ง€๋ฅผ ์ปดํ“จํ„ฐ ์ž…์žฅ์—์„œ ํ™•์‹ ํ•  ์ˆ˜ ์—†๋‹ค.
๋ฌผ๋ก  member("๊ฐ•์Šฌ๊ธฐ", 28, "redvelvet")์ด๋ ‡๊ฒŒ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ
์ด๋ ‡๊ฒŒ ์ง€์ •์„ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ด์ฌ์—์„œ ์•„์˜ˆ ๋ง‰์•„๋‘” ๊ฒƒ ๊ฐ™๋‹ค.

์œ„ ํ•จ์ˆ˜๊ฐ€ ์ž‘๋™ํ•˜๋ ค๋ฉด

def member(age, gropup, member="๊ฐ•์Šฌ๊ธฐ")
    return f"{member}์˜ ๋‚˜์ด๋Š” {age}์ž…๋‹ˆ๋‹ค."

member(28, "๋ ˆ๋“œ๋ฒจ๋ฒณ")

์ง€์ •์ธ์ž๊ฐ€ ์žˆ๋Š” member๋ฅผ ๋’ค๋กœํ•˜๋ฉด ์ž˜ ์ž‘๋™ํ•œ๋‹ค.

๐Ÿš€ ์œ„์น˜ ์ธ์ˆ˜(positional arguments)์™€ ๊ฐ€๋ณ€ ์ธ์ˆ˜(variable length arguments)

  • ์œ„์น˜ ์ธ์ˆ˜(positional arguments): ํ•จ์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ๋Š” ๋ฐฉ์‹

ex.)

def member(name, age, group)

member("๊ฐ•์Šฌ๊ธฐ", 28, "RedVelvet")
  • ๊ฐ€๋ณ€ ์ธ์ˆ˜(variable length arguments) : ์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์€ ๋ฐฉ์‹

ex.)

def member(*args):
    for i in args:
        print(i)

member('1','2','3','4')
>>>1
>>>2
>>>3
>>>4

๊ฐ€๋ณ€์ธ์ˆ˜๋กœ ์„ค์ •๋œ ํ•จ์ˆ˜์— ์œ„์น˜์ธ์ž ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๋„ฃ์œผ๋ฉด ํ•˜๋‚˜์˜ ํŠœํ”Œ๊ฐ์ฒด๋กœ ์ธ์‹ํ•œ๋‹ค.

def member(*args):
    print(type(args))
    
>>> <class 'tuple'>

๊ฐ€๋ณ€์ธ์ˆ˜์˜ ์œ„์น˜

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)

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด

์š”๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.
age์ธ์ž๊ฐ€ ์–ด๋”จ๋ƒ๊ณ  ํ•œ๋‹ค.

์™œ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋–ด์„๊นŒ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋‚ด๊ฐ€ ์ธ์ž๋กœ ๋„˜๊ธด๊ฐ’์— ์–ด๋””๊นŒ์ง€๊ฐ€ *args์ธ์ง€ ํŒŒ์ด์ฌ ์ž…์žฅ์—์„œ๋Š” ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— age์ธ์ž๊ฐ€ ์–ด๋”จ๋ƒ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ •์ƒ์ž‘๋™์„ ์œ„ํ•ด์„œ ์ด๋ ‡๊ฒŒ ๊ณ ์ณ์ค€๋‹ค.

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("์ •์šฐ์„ฑ", "01012341234", "seoul", 20)

>>>name=์ •์šฐ์„ฑ
>>>args=('seoul', 20)
>>>age=01012341234

๋˜๋Š”
func_param_with_var_args("์ •์šฐ์„ฑ", "01012341234", "seoul", age=20)
20์„ age์ธ์ˆ˜์˜ ํ‚ค์›Œ๋“œ๋กœ ์ง€์ •ํ•ด์„œ ๋ณด๋‚ด์ค˜๋„ ๋œ๋‹ค.

๐Ÿ“š ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜ (variable length keyword arguments)

  • ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜: key : valueํ˜•ํƒœ๋กœ ๋œ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

ex.)

def member(**kwargs):
    print(type(kwargs))
    for i in kwargs.items():
        print(i)
    
member(name='์Šฌ๊ธฐ', age=28, group='redvelvet')

>>> <class 'dict'>
>>>('name', '์Šฌ๊ธฐ')
>>>('age', 28)
>>>('group', 'redvelvet')
  • ํ•จ์ˆ˜ ์ธ์ž์ด๋ฆ„์•ž์— **์„ ๋ถ™์ด๋ฉด ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค„์ˆ˜ ์žˆ๊ณ  ํ•จ์ˆ˜๋Š” dictํด๋ž˜์Šค ๊ฐ์ฒด๋กœ ์ธ์‹ํ•œ๋‹ค.

๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜ ์œ„์น˜

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")

๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋„ ๊ฐ€๋ณ€์ธ์ˆ˜์˜ ์œ„์น˜ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„์˜ˆ ํ•จ์ˆ˜ ์„ ์–ธ์ด๋˜์ง€ ์•Š๋Š”๋‹ค.

์ด๊ฒƒ๋„ ๋ณด๋ฉด ํŒŒ์ด์ฌ์—์„œ ์•„๋‹ˆ! ๋„ˆ๊ฐ€ ์ค€ ์ธ์ˆ˜(func_param_with_kwargs("์ •์šฐ์„ฑ", "20", mobile="01012341234", address="seoul"))์—์„œ ์–ด๋””๊นŒ์ง€๊ฐ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜์•ผ! ํ•ด์„œ ํ•จ์ˆ˜ ์„ ์–ธ์ด ์•ˆ๋œ๋‹ค. addfess="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", mobile="01012341234", address="seoul")
  • ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋ฅผ ๋งจ๋’ค๋กœ ๋ณด๋‚ด๋ฉด ํ•ด๊ฒฐ!

์œ„์น˜ ์ธ์ˆ˜์™€ ํ‚ค์›Œ๋“œ ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜ (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")

์•„์ฃผ ์ด์ฒด์  ๋‚œ๊ตญ์ด๋‹ค ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด๊ฑธ ์ข…ํ•ฉํ•ด๋ณด์ž๋ฉด
ํ‚ค์›Œ๋“œ์ธ์ˆ˜๋Š” ์œ„์น˜์ธ์ˆ˜๋ณด๋‹ค ๋’ค์— ์žˆ์–ด์•ผ ํ•˜๊ณ  ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋Š” ๊ฐ€๋ณ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋ณด๋‹ค ์•ž์— ์žˆ์–ด์•ผํ•œ๋‹ค. ์ด๋ฅผ ์‘์šฉํ•ด์„œ ๊ณ ์ณ๋ณด์ž

1ํŠธ

def mixed_params(age, address, name="์•„์ด์œ ", *args, **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")
  • ์ž์‹ ๊ฐ์žˆ๊ฒŒ ๋„์ „ํ–ˆ์ง€๋งŒ address์ธ์ž๋Š” ์–ด๋”จ๋ƒ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋–ด๋‹ค.
    ์•„๋ฌด๋ฆฌ ๋ˆˆ์„์”ป๊ณ  ๋ด๋„ ์™œ ์•ˆ๋ ๊นŒ ํ–ˆ์ง€๋งŒ ํ•˜ํ•„ ๋งˆ์ง€๋ง‰์— address๊ฐ€ ์ธ์ž์˜ ์ด๋ฆ„๊ณผ ๋˜‘๊ฐ™์•„์„œ ํ‚ค์›Œ๋“œ ์ธ์ž์— ์•ˆ๋“ค์–ด ๊ฐ„ ๊ฒƒ ๊ฐ™๋‹ค.

2ํŠธ

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")

์ •๋ฆฌํ•ด๋ณด์ž๋ฉด

age = 20
name="์•„์ด์œ " -> "์ •์šฐ์„ฑ"
*args = "01012341234", "male"
address = "seoul"
**kwargs = mobile = "01012341234"

ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๊ฐ€ ์œ„์น˜์ธ์ˆ˜ ๋’ค์—์žˆ์–ด์•ผํ•˜๊ณ  ํ‚ค์›Œ๋“œ ๊ฐ€๋ณ€์ธ์ˆ˜๋ณด๋‹ค ์•ž์—๋Š” ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ
๊ฐ€๋ณ€์ธ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ ์•ž์ธ์ง€ ๋’ค์ธ์ง€ ํ—ท๊ฐˆ๋ ค์„œ 1ํŠธ์—์„œ ํ‹€๋ ธ๋‹ค.
์ƒ๊ฐํ•ด๋ณด๋‹ˆ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋Š” ์ธ์ž๋กœ ๋„˜๊ฒจ์ค„ ๋•Œ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๋์—ˆ๋‹ค.

ํ›„๊ธฐ

  • ์žฅ๊ณ ๋ฐฐ์šธ ๋•Œ ํ•ญ์ƒ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜์™€ ๊ฐ€๋ณ€์ธ์ˆ˜๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ ๊ณ„์† ๊ณต๋ถ€ํ–ˆ๋Š”๋ฐ ์ด๋ฒˆ์— ๋‹ค์‹œ ์ •๋ฆฌํ•˜๋ฉด์„œ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„์„ ๋” ์•Œ๊ฒŒ ๋๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ