ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
def outer_deco(f):
print("setup")
def wrapper(*a, **kw):
print("call")
return f(*a, **kw)
return wrapper
@outer_deco
def hello():
print("hello")
hello()
hello()- A. setup call hello call hello
- B. setup setup call hello call hello
- C. call hello call hello setup
- D. setup call hello
Correct answer: A. setup call hello call hello
'setup' runs exactly once, when the decorator is applied at definition time. Each call to hello() then re-enters wrapper, printing 'call' followed by 'hello' every time.
More Python Programming questions
- 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}))
- What is printed? python def p(a, b, c=3): return a, b, c d = {"a": 1, "b": 2, "c": 10} print(p(d))
- What is printed? python def r(args): return sum(args) l = [1, 2, 3] print(r(l, l))
- What is printed? python import copy a = [[1, 2], [3, 4]] b = copy.deepcopy(a) b[0][0] = 99 print(a)