Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kwonmha opened this issue Jun 27, 2019 · 0 comments
Open

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

kwonmha opened this issue Jun 27, 2019 · 0 comments

Comments

@kwonmha
Copy link

kwonmha commented Jun 27, 2019

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 "함수가 실행 중입니다."

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant