TIL24. Python : Function Parameters

ID์งฑ์žฌยท2021๋…„ 10์›” 7์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
31/39
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Python์˜ ํ•จ์ˆ˜์— parameter๋ฅผ ์‚ฌ์šฉ ๋•Œ ๋ฐœ์ƒ๋˜๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ Function Parameters

๐Ÿ”ฅ Parameter์˜ ์ข…๋ฅ˜

๐Ÿ”ฅ Parameter ์‚ฌ์šฉ์‹œ ์œ ์˜ํ•  ์ 



1. Parameter์˜ ์ข…๋ฅ˜

๐Ÿค” Keyword Arguments

โœ”๏ธ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉํ•  ๋•Œ, ํŒŒ๋ผ๋ฏธํ„ฐ์—๊ฒŒ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์„œ ์ „๋‹ฌํ•˜๋ฉด ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฅด์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

def keyword_arguments(name, city, age):
    return f"{city}์— ์‚ฌ๋Š” {age}์‚ด {name}์ž…๋‹ˆ๋‹ค."
print(keyword_arguments(age=99, name='jaewon', city='seoul'))  # seoul์— ์‚ฌ๋Š” 99์‚ด jaewon์ž…๋‹ˆ๋‹ค.

๐Ÿค” Mixing positional arguments and keyword arguments

โœ”๏ธ ์ˆœ์„œ๋ฅผ ๋งž์ถ”์–ด์„œ parameter ๊ฐ’์„ ์ „ํ•ด์ฃผ๋Š” positional arguments์™€ keyword arguments๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

def mixed_arguments(name, city, age):
    return f"{city}์— ์‚ฌ๋Š” {age}์‚ด {name}์ž…๋‹ˆ๋‹ค."
print(mixed_arguments("jaewon", age=99, city='seoul'))  # seoul์— ์‚ฌ๋Š” 99์‚ด jaewon์ž…๋‹ˆ๋‹ค.

โœ”๏ธ ๋‹จ, positional arguments์€ ์ˆœ์„œ๋ฅผ ๋งž์ถ”์ง€ ์•Š์œผ๋ฉด SyntaxError๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ์œ„์น˜์— ์œ ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

def mixed_arguments(name, city, age):
    return f"{city}์— ์‚ฌ๋Š” {age}์‚ด {name}์ž…๋‹ˆ๋‹ค."
print(mixed_arguments(age=99, "jaewon", city='seoul'))  # SyntaxError: positional argument follows keyword argument

๐Ÿค” Default Value parameter

โœ”๏ธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, ๋ฐœ์ƒ๋˜๋Š” ์—๋Ÿฌ๋ฅผ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ’์ด ์ „๋‹ฌ๋˜์ง€ ์•Š์„ ๋•Œ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

def default_arguments(name="jaewon"):
    return f"๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค. ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž {name}์ž…๋‹ˆ๋‹ค."
print(default_arguments())  # ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค. ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž jaewon์ž…๋‹ˆ๋‹ค.
print(default_arguments("์žฅ์žฌ์›"))  # ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค. ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž ์žฅ์žฌ์›์ž…๋‹ˆ๋‹ค.

โœ”๏ธ default value parameter์„ ๋จผ์ € ์œ„์น˜์‹œํ‚ค๋ฉด, ๋งจ ์ฒซ parameter๊ฐ€ ์ „๋‹ฌ๋œ๊ฑด์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— SyntaxError๋ฅผ ๋ฑ‰์–ด๋ƒ…๋‹ˆ๋‹ค.

def default_arguments(name="jaewon", age, city):
    return f"{city}์— ์‚ฌ๋Š” {age}์‚ด {name}์ž…๋‹ˆ๋‹ค."
print(default_arguments(age=99, city='seoul'))  # SyntaxError: non-default argument follows default argument

โœ”๏ธ ์ด์— default Value parameter๋Š” ๋ฐ˜๋“œ์‹œ non-default value parameter ๋’ค์— ๋ฐฐ์น˜์‹œ์ผœ์•ผํ•ฉ๋‹ˆ๋‹ค.

def default_arguments(age, city, name="jaewon"):
    return f"{city}์— ์‚ฌ๋Š” {age}์‚ด {name}์ž…๋‹ˆ๋‹ค."
print(default_arguments(age=99, city='seoul'))  # seoul์— ์‚ฌ๋Š” 99์‚ด jaewon์ž…๋‹ˆ๋‹ค.

๐Ÿค” Variable length arguments

โœ”๏ธ parameter์— ์ „๋‹ฌํ•  argument์˜ ์ˆ˜๊ฐ€ ๊ณ ์ •์ ์ด์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ key์™€ value๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์€ ์•„์Šคํƒ€ 2๊ฐœ๊ฐ€ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š”๋ฐ, args์™€ kwags๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

def variable_length_arguments(*args, **kwargs):
    return f"*args : {args},\n**kwargs : {kwargs}"
print(variable_length_arguments("a", "b", 1, 5, 9, name='์žฌ์›', age=99, city="seoul"))
"""
*args : ('a', 'b', 1, 5, 9),
**kwargs : {'name': '์žฌ์›', 'age': 99, 'city': 'seoul'}
"""


2. Parameter ์‚ฌ์šฉ์‹œ ์œ ์˜ํ•  ์ 

๐Ÿค” positional arguments์™€ variable length arguments ์‚ฌ์šฉ์‹œ ์œ ์˜ํ•  ์ 

โœ”๏ธ ์•„๋ž˜ ์ฝ”๋“œ๋Š” positional arguments๊ฐ€ ๋‹ค ์œ„์น˜ํ•˜๊ธฐ ์ „์— variable length arguments๋ฅผ ์ „๋‹ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
โœ”๏ธ 'age'๋ผ๋Š” ์ด๋ฆ„์˜ parameter๊ฐ€ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

def func_param_with_var_args(name, *args, 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) # TypeError: func_param_with_var_args() missing 1 required keyword-only argument: 'age'

โœ”๏ธ ์ด์— positional arguments๋ฅผ ๋จผ์ € ์œ„์น˜์‹œํ‚ค๊ณ , variable length arguments๋ฅผ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

def func_param_with_var_args(name, age, *args):  # ๐Ÿ‘ˆ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด error์—†์ฅฌ???
    print("name=",end=""), print(name) # name=์ •์šฐ์„ฑ
    print("args=",end=""), print(args) # args=('seoul', 20)
    print("age=",end=""), print(age) # age=01012341234
func_param_with_var_args("์ •์šฐ์„ฑ", "01012341234", "seoul", 20)

๐Ÿค” positional arguments & default Value arguments & variable length arguments ์‚ฌ์šฉ์‹œ ์œ ์˜ํ•  ์ 

โœ”๏ธ ์•„๋ž˜ ์ฝ”๋“œ๋Š” SyntaxError: invalid syntax ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๋ฉ๋‹ˆ๋‹ค.

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") # SyntaxError: invalid syntax

โœ”๏ธ positional arguments๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ์œ„์น˜์‹œํ‚ค๊ณ , ๊ทธ ๋‹ค์Œ default Value arguments, variable length arguments๊ฐ€ ๋ฐฐ์น˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
โœ”๏ธ ๊ทธ ์ด์œ ๋Š”

def func_param_with_kwargs(name, age, address=0, **kwargs): # ๐Ÿ‘ˆ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด error์—†์ฅฌ???
    print("name=",end=""), print(name) # name=์ •์šฐ์„ฑ
    print("age=",end=""), print(age) # age=20
    print("kwargs=",end=""), print(kwargs) # kwargs={'mobile': '01012341234'}
    print("address=",end=""), print(address) # address=seoul
func_param_with_kwargs("์ •์šฐ์„ฑ", "20", mobile="01012341234", address="seoul")

๐Ÿค” keyword arguments์™€ variable length arguments ์‚ฌ์šฉ์‹œ ์œ ์˜ํ•  ์ 

โœ”๏ธ ์•„๋ž˜ ์ฝ”๋“œ๋Š” keyword arguments๊ฐ€ variable length 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") # SyntaxError: invalid syntax

โœ”๏ธ keyword arguments์ธ address๋ฅผ *kwargs ์•ž์— ์„ ์–ธํ•œ ์ด์œ ๋Š” address๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ key์™€ value๋กœ ์ „๋‹ฌ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— args ๋ณด๋‹ค ์•ž์„œ ์„ ์–ธ๋˜๋ฉด python ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ๋˜ ํ˜ผ๋ž€์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.

def mixed_params(age, name="์•„์ด์œ ", *args, address, **kwargs):  # ๐Ÿ‘ˆ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด error์—†์ฅฌ???
    print("name=",end=""), print(name) # name=์ •์šฐ์„ฑ
    print("args=",end=""), print(args) # args=('01012341234', 'male')
    print("age=",end=""), print(age) # age=20
    print("kwargs=",end=""), print(kwargs) # kwargs={'mobile': '01012341234'}
    print("address=",end=""), print(address) # address=seoul
mixed_params(20, "์ •์šฐ์„ฑ", "01012341234", "male" ,mobile="01012341234", address="seoul") 
profile
Keep Going, Keep Coding!

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