ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
import time
def stamp(t=[]):
t.append(len(t))
return t
print(stamp(), stamp(), stamp.__defaults__)- A. [0] [0, 1] ([0, 1],)
- B. [0, 1] [0, 1] ([0, 1],)
- C. [0] [0] ([],)
- D. [0, 1] [0, 1] ([],)
Correct answer: B. [0, 1] [0, 1] ([0, 1],)
Both calls return the very same default list, and print receives its arguments only after both calls ran, so both show [0, 1]. __defaults__ holds that same mutated list object.
More Python Programming questions
- What is printed? python x = 5 def f(a, b=x 2, , c=x): return a + b + c x = 100 print(f(1), f(1, c=x))
- What is printed? python def f(a, L=None): if L is None: L = [] L.append(a) return L r = f(1) print(f(2, r), f(3), r)
- What is printed? python class Cart: def init(self, items=[]): self.items = items a = Cart(); b = Cart(); c = Cart([])…
- What is printed? python fs = [] for i in range(3): fs.append(lambda x: x + i) i = 10 print([f(1) for f in fs])
- What is printed? python def make(): fs = [] for i in range(3): fs.append(lambda: i) i = "z" return fs print([f() for f in make()])
- What is printed? python from functools import partial def p(a, b): return a 10 + b fs = [partial(p, i) for i in range(3)] gs = [lambda b…