LearnFrenzy
Sign in Take a free test

Decorators · Predict the Output · Question 3

Decorator code at definition time Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following Python program?

main.py
def register(func):
    print("registering", func.__name__)
    return func

@register
def a(): return "A"

@register
def b(): return "B"

print("start")
print(a() + b())
  • A start registering a registering b AB
  • B registering a registering b start AB Correct
  • C start AB
  • D registering a start registering b AB
Correct Answer: Option B

Decorators run as each def executes, before the rest of the program; register returns the functions unchanged.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links