ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
def deco2(f):
print("decorating", f.__name__)
def wrapper(*a, **kw):
return f(*a, **kw)
return wrapper
@deco2
def foo():
pass
print("done")- A. done decorating foo
- B. decorating foo done
- C. decorating wrapper done
- D. done
Correct answer: B. decorating foo done
The decorator runs immediately when the def is executed (at import/definition time), not when foo is later called, so 'decorating foo' prints before 'done'.
More Python Programming questions
- What is printed? python counter = 0 def countcalls(f): def wrapper(a, kw): global counter counter += 1 return f(a, kw) return wrapper…
- What is printed? python def applytwice(f): def wrapper(x): return f(f(x)) return wrapper @applytwice def inc(x): return x + 1 print(inc(5))
- What is printed? python def outerdeco(f): print("setup") def wrapper(a, kw): print("call") return f(a, kw) return wrapper @outerdeco def…
- What is printed? python def h(a, b, c): return a + b + c args = [1, 2, 3] print(h(args)) args2 = [1, 2] try: print(h(args2)) except…
- What is printed? python def k(a, , b): return a + b try: k(1, 2) except TypeError: print("TypeError") print(k(1, b=2))
- What is printed? python def n(kw): return kw print(n({"a": 1}, {"b": 2}))