ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
def decorator_with_args(prefix):
def actual(f):
def wrapper(*a, **k):
return prefix + str(f(*a, **k))
return wrapper
return actual
@decorator_with_args("result: ")
def add(a, b):
return a + b
print(add(2, 3))- A. 5
- B. result: 5
- C. TypeError
- D. add(2, 3)
Correct answer: B. result: 5
decorator_with_args('result: ') is called first and returns actual, the real decorator, which wraps add so every call prefixes the string 'result: ' onto the stringified sum: 'result: 5'.
More Python Programming questions
- 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()})
- What is printed? python s1 = {1, 2, 3} s2 = {2, 3, 4} print(s1 - s2, s1 | s2, s1 & s2)