LearnFrenzy
Sign in Take a free test

Decorators · Predict the Output · Question 6

The order of stacked decorators Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Python program?

main.py
def add_a(f):
    return lambda: f() + "a"

def add_b(f):
    return lambda: f() + "b"

@add_a
@add_b
def base():
    return "x"

print(base())
  • A xba Correct
  • B xab
  • C abx
  • D bax
Correct Answer: Option A

base = add_a(add_b(base)): add_b appends "b" to "x" first, then add_a appends "a".

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