ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def f():
return f.n
f.n = 5
g = f
f = lambda: 0
try:
print(g())
except AttributeError:
print("AE", g.n)- A. 5
- B. 0
- C. AE 0
- D. AE 5
Correct answer: D. AE 5
The body of the original function looks up the global name f at call time, which now refers to the lambda, and the lambda has no attribute n. The attribute still lives on the original function object, reachable as g.n.
More Python Programming questions
- What is printed? python words = ["kiwi", "fig", "apple", "date"] print(sorted(words, key=len), max(words, key=len), min(words))
- What is printed? python import copy class Node: def init(self): self.kids = [] a = Node() a.kids.append(a) b = copy.deepcopy(a)…
- What is printed? python a = [1, [2, 3]] b = a[:] c = list(a) d = [a] a[1] += [4] a[0] += 10 print(b, c is d, d[1] is a[1])
- What is printed? python s = "abc" it = iter(s) print(next(it), "".join(it), list(it), [c for c in s if c in "ab" or c == "z"])
- What is printed? python def f(n): if n == 0: return [] return [n] + f(n - 1) if n % 2 else f(n - 1) + [n] print(f(5))
- What is printed? python x = 1 def f(): print(x, end=" ") def g(): x = 2 f() g() h = lambda: x x = 3 print(h())