ProgrammingHardAccenture pattern
What is printed?
What is printed?
def deco(f):
def wrapper(*a, **k):
wrapper.calls += 1
return f(*a, **k)
wrapper.calls = 0
return wrapper
@deco
def h():
return 1
h(); h(); h()
print(h.calls)- A. 0
- B. 1
- C. 3
- D. AttributeError
Correct answer: C. 3
wrapper.calls is a plain function attribute attached to the wrapper object itself, which h now refers to. Each call increments it, so after three calls h.calls is 3.
More Python Programming questions
- What is printed? python def decoratorwithargs(prefix): def actual(f): def wrapper(a, k): return prefix + str(f(a, k)) return wrapper…
- What is printed? python class A: def init(self): self.x = 1 def enter(self): return self def exit(self, a): self.x = 0 return False with…
- What is printed? python class R: @property def x(self): return 5 r = R() try: r.x = 10 except AttributeError: print("AttributeError")
- What is printed? python try: raise TypeError("t") except (ValueError, TypeError) as e: print("caught", type(e).name)
- What is printed? python def f(): return 1, 2, 3 a, b, c = f() print(a, b, c) x = f() print(type(x), x)
- What is printed? python d = {"a": 1, "b": 2} print({v: k for k, v in d.items()})