LearnFrenzy
Sign in Take a free test

Decorators · Predict the Output · Question 5

A decorator with an argument Answer, explanation & discussion

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

What is the output of the following Python program?

main.py
def repeat(n):
    def deco(func):
        def wrapper(*args):
            return [func(*args) for _ in range(n)]
        return wrapper
    return deco

@repeat(2)
def hi(name):
    return "hi " + name

print(hi("Asha"), len(repeat(5)(len)("abc")))
  • A hi Asha 5
  • B ['hi Asha', 'hi Asha'] 3
  • C TypeError
  • D ['hi Asha', 'hi Asha'] 5 Correct
Correct Answer: Option D

repeat(5)(len) decorates len by hand; calling it with "abc" returns a list of five 3s, whose length is 5.

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