함수 #python #tutorial
값을 순서대로 상응하는 매개변수에 복사하는 인자이다.
def menu(wine, entree, dessert):
return {'wine': wine, 'entree': entree, 'dessert': dessert}
매개변수에 상응하는 이름을 인자에 지정할 수 있다.
menu(entree='beef', dessert='bagel', wine='bordeaux')
위치인자와 키워드 인자는 섞어서 쓸 수 있다. 단 위치 인자와 키워드 인자로 함수를 호출한다면 위치 인자가 먼저 와야 한다.
menu('frontenac', dessert='flan', entree='fish')
def menu(wine, entree, dessert='pudding'):
return {'wine': wine, 'entree': entree, 'dessert': dessert}
menu('chardonnay', 'chicken')
함수의 매개변수에 *를 사용하면 위치 인자 변수들을 튜플로 묶는다
def print_args(*args):
print('Positional argument tuple:', args)
print_args() # ()
print_args(3, 2, 1, 'wait!', 'uh...') # (3, 2, 1, 'wait!', 'uh...')
매개변수에 맨끝에 *args 를 써서 나머지 인자를 모두 취하게 할 수 있다.
def print_more(required1, required2, *args):
print('Need this one:', required1)
print('Need this one too:', required2)
print('All the rest:', args)
print_more('cap', 'gloves', 'scarf', 'monocle', 'mustache wax')
키워드 인자를 딕셔너리로 묶을 수 있다. 인자의 이름은 키고, 값은 이 키에 대응하는 딕셔너리 값이다.
def print_kwargs(**kwargs):
print('Keywor arguments:', kwargs)
print_kwargs(wine='merlot', entree='mutton', dessert='macaron')
# Keyword arguments: {'dessert': 'macaroon', 'wine': 'merlot', 'entree': 'mutton'}
위치 매개변수와 *args, **kwargs를 섞어서 사용하려면 이들을 순서대로 배치해야 한다.
함수 몸체 시작 부분에 문자열을 포함시켜 문서를 붙일 수 있다.
def echo(anything):
'echo returns its input argument'
return anything
docstring은 길게 작성할수 있고, 서식을 추가할 수도 있다.
def print_if_true(thing, check):
'''
Prints the first argument if a second argument is true.
The operation is:
1. Check whether the *second* argument is true.
2. If it is, print the *first* argument.
'''
if check:
print(thing)
docstring은 help(함수이름)으로 출력할 수 있다. 서식없는 docstring을 그대로 보고 싶다면 print(함수이름._doc) 으로 출력한다. _doc은 docstring의 내부 이름인 함수 내의 변수이다.
함수는 변수에 할당할 수 있고, 다른 함수에서 인자로 사용될 수 있으며 이를 반환하는것도 가능하다.
def answer():
print(42)
def run_something(func):
func()
run_something(answer) # 42
type(run_something) # <class 'function'>
def add_args(arg1, arg2):
print(arg1 + arg2)
def run_something_with_args(func, arg1, arg2):
func(arg1, arg2)
run_something_with_args(add_args, 5, 9) # 14
또한 *args, **kwargs 인자와 결합하여 사용할 수 있다.
def sum_args(*args):
return sum(args)
def run_with_positional_args(func, *args):
return func(*args)
run_with_positional_args(sum_args, 1, 2, 3, 4) # 10
함수는 리스트, 튜플, 셋, 딕셔너리의 요소로 사용할 수 있다. 함수는 불변이므로 딕셔너리의 키로도 사용할 수 있다.
함수 안에 또 다른 함수를 정리할 수 있다.
def outer(a, b):
def inner(c, d):
return c + d
return inner(a, b)
outer(4, 7) # 11
클로져는 다른 함수에 의해 동적으로 생성된다. 그리고 바깥 함수로부터 생성된 변수값을 변경하고, 저장할 수 있는 함수이다.
def knights2(saying):
def inner2():
return "We are the knights who say: '%s'" % saying
return inner2
a = knights2('Duck')
b = knights2('Hasenpfeffer')
a, b의 타입은 함수이며 이들은 함수이지만 클로져이기도 하다.
type(a) # <class 'function'>
type(b) # <class 'function'>
a # <function kights2.<locals>.inner2 at 0x10193e158>
b # <function kights2.<locals>.inner2 at 0x10193e1e0>
이들을 호출하면 knights2() 함수에 전달되어 사용된 saying을 기억하다.
a() # "We are the knights who say: 'Duck'"
b() # "We are the knights who say: 'Hasenpfeffer'"
def edit_story(words, func):
for word in words:
print(func(word))
staris = ['thud', 'meow', 'thud', 'hiss']
def enliven(word): # 첫 글자를 대문자로 만들고 느낌표 붙이기
return word.capitalize() + '!'
edit_story(stairs, enliven)
위 예제를 람다로 바꾸면
edit_story(stairs, lambda word: word.capitalize() + '!')
람다의 콜론(:)와 닫는 괄호 사이에 있는 것이 함수의 정의 부분이다. 콜백 함수를 정의하는 GUI에서 람다를 사용할 수 있다.