Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active February 25, 2021 08:55
Show Gist options
  • Save nattybear/2d4c0d3dd222e9c8afd1faa564775b4c to your computer and use it in GitHub Desktop.
Save nattybear/2d4c0d3dd222e9c8afd1faa564775b4c to your computer and use it in GitHub Desktop.
파이썬 : 인자로 함수를 받는 함수

인자로 함수를 받는 함수

질문자님이 알고 계신 것처럼 파이썬에서 함수를 호출할 때는 함수 이름을 적고 괄호를 적습니다.

int("10")

그런데 함수를 직접 호출하는 게 아니라 어떤 함수의 인자에 함수를 넣을 때는 괄호를 적지 않고 함수 이름만 적습니다.

xs = ["1", "2"]

map(int, xs)

함수를 인자로 받는 함수를 직접 만들어 보면 아래와 같습니다.

def myMap(f, xs):
  ys = []
  for x in xs:
    y = f(x)  # 인자로 받은 함수 이름 f를 활용
    ys.append(y)
  return ys
  
  
xs = ["1", "2"]

ys = myMap(int, xs)

print(ys)  # [1, 2]

이렇게 인자로 함수를 받는 map 같은 함수를 Higher Order Function 이라고 합니다.

감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment