Skip to content

7.5 Decorator 설명 중 소스 코드의 청사진 부분 관련 #18

@kwonmha

Description

@kwonmha
from functools import wraps
def decorator_name(f):
  @wraps(f)
  def decorated(*args, **kwargs):
    if not can_run:
      return "함수는 실행되지 않습니다."
    return f(*args, **kwargs)
  return decorated

@decorator_name
def func():
  print "함수가 실행 중입니다."

can_run = True
print(func())
#output: 함수가 실행 중입니다

위의 소스처럼 can_runTrue인 상태에서 print(func())을 실행하면 실행 결과는

함수가 실행 중입니다.
None

으로 None까지 같이 출력됩니다. def decorated()의 return이 f()이고, f에 해당하는 func()는 return값이 없기 때문에요.
차라리 func()

@decorator_name
def func():
  return "함수가 실행 중입니다."

으로 하는 게 더 낫지 않을까요?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions